The icanboogie/bind-routing package binds ICanBoogie/Routing to ICanBoogie. It provides infrastructure to configure routes and responders, a trait to get URLs from objects, and commands to list routes and actions.
composer require icanboogie/bind-routingRoutes are defined by controllers, with the attributes of icanboogie/routing such as Get or Post. The class
attribute Prefix defines the prefixes shared by the methods. If left undefined, the action is inferred from the
controller class and the method name (SkillController::show is skill:show, names are not pluralized).
<?php
namespace App\Presentation\HTTP;
use ICanBoogie\HTTP\Response;
use ICanBoogie\Routing\Attribute\Get;
use ICanBoogie\Routing\Attribute\Post;
use ICanBoogie\Routing\Attribute\Prefix;
#[Prefix(pattern: '/skills')]
final class SkillController
{
// This will create a 'GET /skills' route with 'skill:list' action
#[Get]
public function list(): Response
{
// …
}
// This will create a 'GET /skills/:slug' route with 'skill:show' action
#[Get('/:slug')]
public function show(string $slug): Response
{
// …
}
// This will create a 'POST /skills' route with 'skill:create' action
#[Post]
public function create(): Response
{
// …
}
}Controllers are discovered with olvlvl/composer-attribute-collector, and registered as shared services:
<?php
// app/all/config/routes.php
namespace App;
use ICanBoogie\Routing\ConfigBuilder;
use ICanBoogie\Routing\CacheStrategy;
return fn(ConfigBuilder $config) => $config
->enable_discovery()
// Meant for production, use CacheStrategy::MEMOIZE in development.
->cache_strategy(CacheStrategy::COMPILE);Use add_controller() to add a controller by hand, with an optional route builder instead of attributes. Such a
controller must be tagged with routing.controller, so it can be obtained from the container:
<?php
use ICanBoogie\Routing\ConfigBuilder;
use ICanBoogie\Routing\RouteBuilder;
return fn(ConfigBuilder $config) => $config
->add_controller(ContactController::class, fn(RouteBuilder $r) => $r
->get('/contact.html', 'new')
->post('/contact.html', 'create'));services:
App\Presentation\HTTP\ContactController:
tags: [ routing.controller ]In development, use CacheStrategy::MEMOIZE.
The trait UrlTrait adds a url() method and a $url property to a prototyped object. The action is inferred from
the class of the object, the way it is for controllers, and the properties of the object provide the values of the
route pattern: $article->url is the URL of the route article:show, and $article->url('list') the one of
article:list.
Names are not pluralized. When the controller is not named after the object, e.g. ArticlesController for Article,
mark the class with the attribute Name:
<?php
namespace App\Articles;
use ICanBoogie\Binding\Routing\Prototype\UrlTrait;
use ICanBoogie\Prototyped;
use ICanBoogie\Routing\Attribute\Name;
#[Name('articles')]
final class Article extends Prototyped
{
use UrlTrait;
public function __construct(
public readonly string $slug,
) {
}
}
$article = new Article('hello-world');
$article->url; // the URL of 'articles:show', e.g. "/articles/hello-world"The attribute applies to the class that has it, its subclasses don't inherit it.
The url method is bound to the objects with the prototype configuration:
<?php
// app/all/config/prototype.php
namespace App;
use ICanBoogie\Binding\Routing\Prototype\UrlMethod;
use ICanBoogie\Prototype\ConfigBuilder;
return fn(ConfigBuilder $config) => UrlMethod::bind($config);Middleware, see icanboogie/routing, wraps the responders, e.g. to require authentication. A
service implementing ICanBoogie\Routing\Middleware is tagged with routing.middleware when it is
registered with autoconfigure: true, and collected by the
ICanBoogie\Routing\MiddlewareCollection service, which wraps the responders provided by the
ActionResponderProvider service. There is nothing else to configure:
<?php
namespace App\Presentation\HTTP;
use ICanBoogie\HTTP\Responder;
use ICanBoogie\Routing\Middleware;
final class RequireAuthentication implements Middleware
{
public function responder(Responder $next): Responder
{
// …
}
}services:
_defaults:
autowire: true
autoconfigure: true
App\Presentation\HTTP\RequireAuthentication: ~Without autoconfigure, tag the service with routing.middleware yourself. The first middleware is the closest to the
endpoint, each following one wraps the previous. Order them with the priority of the tag, the highest first:
services:
App\Presentation\HTTP\RequireAuthentication:
tags: [ { name: routing.middleware, priority: 10 } ]The middleware of icanboogie/routing are not registered, register the ones you use. For instance,
for #[Part] to read a JSON body, register DecodeJsonBody, with a priority lower than that of the
middleware that reads request_params, so that it runs before them:
services:
ICanBoogie\Routing\Middleware\DecodeJsonBody:
tags: [ { name: routing.middleware, priority: -100 } ]Controllers extending ControllerAbstract still work, but they have to be registered and tagged with
action_responder and action_alias, their routes have to be provided separately:
services:
_defaults:
autowire: true
App\Presentation\HTTP\Controller\ArticleController:
shared: false
tags:
- { name: action_responder }
- { name: action_alias, action: 'articles:list' }
- { name: action_alias, action: 'articles:show' }The following code demonstrates how to obtain a route provider from the routes configuration:
<?php
namespace ICanBoogie;
/** @var Application $app */
$config = $app->config_for_class(Routing\Config::class);
$routes = $config->routes;The project is continuously tested by GitHub actions.
This project adheres to a Contributor Code of Conduct. By participating in this project and its community, you're expected to uphold this code.
See CONTRIBUTING for details.