icanboogie / bind-routing
Binds icanboogie/routing to ICanBoogie.
Installs: 3 649
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=7.2
- icanboogie/bind-http: ^5.0
- icanboogie/icanboogie: ^5.0
- icanboogie/routing: ^5.0
Requires (Dev)
- phpunit/phpunit: ^8.5
README
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.
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
.
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.
<?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:
<?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
.
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.
<?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.
The following example demonstrates how ArticleControler
is configured to handle the actions
articles:show
and articles:list
.
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:
<?php namespace ICanBoogie; /** @var Application $app */ $routes = $app->config_for_class(Routing\RouteProvider::class);
Continuous Integration
The project is continuously tested by GitHub actions.
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.
Contributing
See CONTRIBUTING for details.