icanboogie / bind-routing
Binds icanboogie/routing to ICanBoogie.
Installs: 3 441
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
This package is auto-updated.
Last update: 2023-09-09 10:17:19 UTC
README
The icanboogie/bind-routing package binds icanboogie/routing to ICanBoogie.
The package adds the getter routes
to the Application instance, a url_for()
method that
creates URLs, and a synthesizer for the routes
configuration fragments.
<?php namespace ICanBoogie; require 'vendor/autoload.php'; $app = boot(); # # Get routes configuration # $config = $app->configs['routes']; # # Get the route collection and add a new route # use ICanBoogie\HTTP\Request; use ICanBoogie\Routing\RouteDefinition; $app->routes->get('/hello', function(Request $request) { $who = $request['name'] ?: 'world'; return "Hello $who!"; }, [ RouteDefinition::ID => 'hello' ]); # # Obtain de URL of an article # echo $app->url_for('articles:show', $app->models['articles']->one);
Defining routes using configuration fragments
The most efficient way to define routes is through routes
configuration fragments, because it
doesn't require application logic (additional code) and the synthesized configuration may be cached.
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\Routing\RouteDefinition; use ICanBoogie\Routing\RouteMaker as Make; return [ 'home' => [ RouteDefinition::PATTERN => '/', RouteDefinition::CONTROLLER => PagesController::class, RouteDefinition::ACTION => Make::ACTION_INDEX ] ] + array_replace_recursive(Make::resource('articles', ArticlesController::class), [ 'articles:show' => [ RouteDefinition::PATTERN => '/articles/:year-:month-:slug.html' ] ]);
The following code demonstrates how the synthesized routes
configuration can be obtained:
<?php $routes_config = $app->configs['routes'];
Note: To make it easy for you to find where routes are defined, the pathname to the configuration fragment is set as
__ORIGIN__
in the route definition.
Before the configuration is synthesized
The routing.synthesize_routes:before
event of class BeforeSynthesizeRoutesEvent is fired
before the configuration is synthesized. Event hooks may use this event to alter the configuration
fragments before they are synthesized.
The following example demonstrates how the routing.synthesize_routes:before
event can be used to
alter the patterns of the route definitions before they synthesized:
<?php use ICanBoogie\Binding\Routing\BeforeSynthesizeRoutesEvent; use ICanBoogie\Routing\RouteDefinition; $app->events->attach('routing.synthesize_routes:before', function(BeforeSynthesizeRoutesEvent $event) { foreach ($event->fragments as &$fragment) { foreach ($fragment as &$definition) { $definition[RouteDefinition::PATTERN] = '/en' . $definition[RouteDefinition::PATTERN]; } } });
The configuration is synthesized
The routing.synthesize_routes
event of class SynthesizeRoutesEvent is fired when the
configuration is synthesized. Event hooks may use this event to alter the synthesized configuration
before it is returned by the synthesizer.
Requirements
The package requires PHP 7.2 or later.
Installation
composer require icanboogie/bind-routing
Documentation
The package is documented as part of the ICanBoogie framework documentation. You can
generate the documentation for the package and its dependencies with the make doc
command. The
documentation is generated in the build/docs
directory. ApiGen is required.
The directory can later be cleaned with the make clean
command.
Testing
Run make test-container
to create and log into the test container, then run make test
to run the
test suite. Alternatively, run make test-coverage
to run the test suite with test coverage. Open
build/coverage/index.html
to see the breakdown of the code coverage.
License
icanboogie/bind-routing is released under the New BSD License.