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.
<?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.
<?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.