Skip to content

Internals Bootstrap Routes Router

Stefan Crkvenjakov edited this page Oct 7, 2026 · 1 revision

bootstrap/routes/router.php

Framework Internals · Subsystem overview · Developer guide

Source: bootstrap/routes/router.php · Snapshot 8411f4287706.

Bootstrap scripts wire globals, constants and collaborators in a defined order. These files are executable entry points rather than a reusable class API.

Imports and dependencies

use redirect\Redirect;
use response\Response;

Execution and global state

The script below is executed by its caller. Constants, registered callbacks, global assignments, guards, included files and their order are preserved here. Unlike class methods, entry-point execution can start the CLI loop, route a request, emit output, or terminate the process.

<?php
use redirect\Redirect;
use response\Response;

global $loader;
global $defaultRouters;
global $routerManager;
global $eventDispatcher;

$routerManager->addEventDispatcher($eventDispatcher);

foreach ($loader->routers as $router) {
    $routerManager->loadRouters($router->getRouters());
}

foreach ($defaultRouters as $route) {
    $routerManager->addRouter($route);
}

if (PHP_SAPI === 'cli')
    return $routerManager;

// HEAD runs the normal route pipeline and sends headers, but never a body.
$request = (new \router\http\GlobalsRequestProvider())->current();
if ($request->method() === 'HEAD') {
    ob_start(static fn(string $body): string => '');
}

$page = $routerManager->startRouter($request);

try {
    if ($page instanceof Redirect) {
        $redirect = $page->getRedirectTo();
        header("Location: $redirect");
        exit();
    }

    if($page instanceof Response) {
        $page->send();
        exit();
    }

    $doc = $page->render();
} catch (Exception $e) {
    die("Internal error: " . $e->getMessage());
}

echo $doc;

$redirect = new Redirect();
$redirect->setLast();

return $routerManager;

Related types

Clone this wiki locally