Routing

Routes connect HTTP requests to your code. The router lives on $App->router and supports the standard verbs plus a catch-all any().

HTTP verbs

One method per HTTP verb, plus any and map for the multi-verb cases:

$App->router->get   ('/users',          $handler);
$App->router->post  ('/users',          $handler);
$App->router->put   ('/users/:id',      $handler);
$App->router->patch ('/users/:id',      $handler);
$App->router->delete('/users/:id',      $handler);
$App->router->options('/users',         $handler);

$App->router->any('/webhook',           $handler);          // any verb
$App->router->map(['GET','POST'], '/x', $handler);          // multi

Handlers

A handler is one of:

  • A closure — inline code, given $request as its argument:
    $App->router->get('/ping', function($request) use ($App) {
        $App->response('pong', 'text');
    });
  • A path to a controller file — the file is included with $App and $request in scope:
    $App->router->get('/contact', BP.'/app/controllers/contact.php');
  • Any callable — function name, [$obj, 'method'], etc.

Passing extra values

Pass an array as the third argument and it's spread into the handler after $request:

$App->router->get('/profile', function($request, $userId) use ($App) {
    // $userId === 42
}, [42]);

The catch-all

The starter app/routes.php ends with a catch-all that turns every URL into a "find the controller and view by convention" lookup:

$App->router->any('/*:path', function($request) use ($App) {
    $vars = [];
    @include $App->router->controller($request->route->path);
    $App->view->render($request->route->path . '.phtml', $vars);
});

This means a request for /about will:

  1. Try to include app/controllers/about.php (silent if it doesn't exist)
  2. Render app/views/about.phtml with whatever $vars the controller set

Most "informational" pages need no explicit route at all — drop a controller and a view in place and the URL works.

Force HTTPS

Call this before any routes are matched (typically near the top of routes.php):

$App->router->makeHttps();

Static redirects

Keep a JSON file of legacy URLs and let the router emit 301s for you:

$App->router->redirect([
    'request' => $_SERVER['REQUEST_URI'],
    'lookup'  => BP.'/app/data/redirects.json',
    'type'    => '301',
]);