LEMONADE Documentation

Documentation.

Fifteen chapters covering installation, project structure, application development, CLI and production deployment.

Chapter 4 of 15

Routing and URLs.

Connect an HTTP method and URL to an explicit controller action, then generate links from route names.

Named routes

Routes live in app/Config/Routing.php. Always create handlers with ControllerAction::for(); the framework does not support Controller@action strings or URL-derived controller conventions.

app/Config/Routing.php
<?php

use App\Controllers\ArticleController;
use Lemonade\Framework\Routing\ControllerAction;
use Lemonade\Framework\Routing\Router;

return static function (Router $router): void {
    $router->getNamed(
        'articles.detail',
        '/articles/{id}',
        ControllerAction::for(ArticleController::class, 'detail'),
    );
};

The {id} parameter is passed to the action argument with the same name. Use constrainParameter() to restrict accepted values.

URL generation

Inject UrlGenerator outside templates. Use $this->url() in templates. You can then change a route path without hunting down hard-coded links.

URL generation
<?php

use Lemonade\Framework\Routing\UrlGenerator;

final readonly class ArticleLinks
{
    public function __construct(private UrlGenerator $urls) {}

    public function detail(int $id): string
    {
        return $this->urls->route('articles.detail', ['id' => $id]);
    }
}

Parameters matching route segments fill the URL; any extra values become a query string.

Detailed technical documentation on GitHub