1 Adding a New Page
stak administrator edited this page 2026-09-10 11:42:56 +00:00

Adding a New Page

This walks through adding a new protected page, using pages/role1_area.php as the template.

1. Create the page file

Create a new file under pages/, e.g. pages/my_page.php:

<?php
require_once __DIR__ . '/../app/bootstrap.php';

$pageTitle = 'My Page';
require __DIR__ . '/../includes/header.php';
?>
<h1>My Page</h1>
<p>Your content here.</p>

<?php
require __DIR__ . '/../includes/footer.php';

Rules to follow:

  • Always start with require_once __DIR__ . '/../app/bootstrap.php'; — this gives you the database connection, session, and all helper functions (isLoggedIn(), currentUser(), url(), redirectTo(), setFlash(), etc.).
  • Always use url('/path') for links, form action attributes, and redirects — never a hardcoded /path. This is what keeps the app working regardless of which subfolder it's installed in (see Architecture).
  • If your page queries the database directly ($pdo->query(...) / $pdo->prepare(...)) rather than through an app/auth.php helper, add global $pdo; right after the bootstrap require.

2. Register the route

Add an entry to config/routes.php:

'/my-page' => [
    'file'  => 'pages/my_page.php',
    'roles' => ['role1', 'role2'], // adjust as needed
],

That's it — no other file needs to change. The router picks up the new route automatically.

If the page should be reachable from the main navigation, add a link in includes/header.php, following the existing pattern:

<a href="<?= url('/my-page') ?>">My Page</a>

Wrap it in the appropriate condition (isLoggedIn(), isAdmin(), ...) if it shouldn't always be shown.

Common patterns

A page that requires a login but no specific role:

'roles' => ['AUTH'],

A form that writes to the database: Follow the pattern in pages/admin_user_new.php — validate input, collect errors into an array, only run the INSERT/UPDATE once $errors is empty, then setFlash(...) and redirectTo(...).

A page that needs a record by ID from the query string:

$id = (int) ($_GET['id'] ?? 0);
$record = findUserById($id); // or your own finder function
if (!$record) {
    setFlash('Not found.', 'error');
    redirectTo('/somewhere');
}