bind-routing
, (*1)
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., (*2)
Installation
composer require icanboogie/bind-routing
Defining routes using attributes
The easiest way to define routes is to use attributes such as Route or Get to tag your controller and actions. Using any of these tags triggers the registration of the controller as a service (if it is not already registered), and the tagging with action_responder
and action_alias
., (*3)
The following example demonstrates how the Route attribute can be used at the class level to specify a prefix for all the actions of a controller. The Get and [Post][] attributes are used to tag actions. If left undefined, the action is inferred from the controller class and the method name., (*4)
<?php
namespace App\Presentation\HTTP
use ICanBoogie\Accessor\AccessorTrait;use ICanBoogie\Binding\Routing\Attribute\Get;use ICanBoogie\Binding\Routing\Attribute\Route;use ICanBoogie\Routing\ControllerAbstract;
#[Route('/skills')]
final SkillController extends ControllerAbstract
{
use AccessorTrait;
// This will create a 'GET /skills' route with 'skills:list' action
#[Get]
private function list(): void
{
// …
}
// This will create a 'GET /skills/:slug' route with 'skills:show' action
#[Get('/:slug')]
private function show(string $slug): void
{
// …
}
// This will create a 'POST /skills' route with 'skills:create' action
#[Post]
private function create(): void
{
// …
}
}
Use the use_attributes()
method to configure the builder using attributes:, (*5)
<?php
// app/all/config/routes.php
namespace App;
use ICanBoogie\Binding\Routing\ConfigBuilder;
return fn(ConfigBuilder $config) => $config->use_attributes();
Defining routes using configuration fragments
Alternatively, you can configure routes manually using routes
configuration fragments, but you will have to register the service and tag it with action_responder
and action_alias
., (*6)
The following example demonstrates how to define routes, resource routes. The pattern of the articles:show
route is overridden to use year, month and slug., (*7)
<?php
// config/routes.php
namespace App;
use ICanBoogie\Binding\Routing\ConfigBuilder;
use ICanBoogie\Routing\RouteMaker;
return fn(ConfigBuilder $config) => $config
->route('/', 'page:home')
->resource('articles', new Make\Options(
basics: [
RouteMaker::ACTION_SHOW => new Make\Basics('/articles/:year-:month-:slug.html')
]
));
Matching routes with controllers
Routes have no idea of the controller to use, to match a route with a controller, you need to tag
the controller with the actions that it supports., (*8)
The following example demonstrates how ArticleControler
is configured to handle the actions
articles:show
and articles:list
., (*9)
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' }
Getting route configuration
The following code demonstrates how to obtain a route provider from the routes configuration:, (*10)
<?php
namespace ICanBoogie;
/** @var Application $app */
$routes = $app->config_for_class(Routing\RouteProvider::class);
Continuous Integration
The project is continuously tested by GitHub actions., (*11)
, (*12)
Code of Conduct
This project adheres to a Contributor Code of Conduct. By participating in
this project and its community, you're expected to uphold this code., (*13)
Contributing
See CONTRIBUTING for details., (*14)