1. INTRODUCTION

Every page request passes through index.php, which starts the framework. The router then finds the method for the requested URL and calls it with any extra URL parts, such as domain.com/home/var1/var2/var3.

Your route methods belong in App/Backend/Routes/Routes.php. This class extends BackendCoreRouter.

2. DEFINING ROUTES

Use addRoute() to connect a URL path to a method in your Routes class. The first value is the path; the second is the method name.

The following example adds a Home route and uses the same method for the homepage and fallback. Add these methods inside your Routes class, updating its existing constructor:

public function __construct()
{
    // Use home() for the homepage and fallback.
    parent::setHomepageHandler("home");
    parent::setDefaultHandler("home");

    // Visiting domain.com/home calls home().
    parent::addRoute("home", "home");

    parent::handleRoutes();
}

public function home(): void
{
    debug("we are here!");
}

With ENABLE_LANGUAGES set to false, open /home/ to try it. Enable ENABLE_DEBUG_DISPLAY and keep PRODUCTION_MODE set to false to see the debug message. Register your routes before calling handleRoutes(). The framework creates the Routes object for you.

Using Language Prefixes

If languages are enabled, replace the single addRoute() line with:

foreach (AVAILABLE_LANGUAGES as $language)
{
    parent::addRoute($language . "/home", "home");
}

This creates paths such as /en/home/ and /fi/home/. Homepage language paths are added automatically by setHomepageHandler().

!
Note!

A route also matches extra URL parts: /home/hello still calls home(). Unknown paths can reach the homepage method too. Check any extra arguments if you want to show a 404 page instead of the homepage.

3. HANDLING ROUTE VARIABLES

The router passes extra URL parts directly to your method arguments, in order. For /home/Alex/, the first argument receives Alex.

Replace the earlier home() method with this example to read one value:

public function home(string $name = "Visitor"): void
{
    debug(rawurldecode($name)); // /home/Alex/ prints "Alex".
}

To accept three values, use three arguments instead:

public function home(
    ?string $first = null,
    ?string $second = null,
    ?string $third = null
): void {
    // /home/one/two/three/
    debug($first);  // "one"
    debug($second); // "two"
    debug($third);  // "three"
}

Default values let visitors open a shorter URL without causing a missing-argument error. rawurldecode() converts encoded characters such as %20 back into spaces.

⚠
Important!

URL values are not automatically cleaned or HTML-escaped. Check them before use, and call esc_html() when displaying them as HTML text.

If you register a longer path such as home/details, it takes priority over home. Query values such as ?page=2 are available in $_GET rather than method arguments.

4. RENDERING PAGES

To display a page, call render() from your route method. Replace the earlier home() method with:

public function home(): void
{
    // Loads App/templates/home.php.
    $this->render("home");
}

Create home.php in App/templates/ and add your page content. The shared header and footer are included automatically.

⚠
Important!

Call render() once per page response. Use includes inside the template for smaller page sections.

Passing Variables to Views

You can pass variables as an array in the second argument of render(). Here’s a version that passes a welcome message and a name from the URL:

public function home(string $name = "Visitor"): void
{
    $this->render("home", [
        "message" => "Welcome",
        "name" => rawurldecode($name),
    ]);
}

In App/templates/home.php, use the key names to access these values:

<h1><?= esc_html($message); ?></h1>
<p>Hello, <?= esc_html($name); ?>!</p>

Visiting /home/Alex/ now displays “Welcome” and “Hello, Alex!”. Use descriptive names for your own variables; page and kristal_metadata are already used by the renderer.