Download the PHP package echo-fusion/routemanager without Composer
On this page you can find all versions of the php package echo-fusion/routemanager. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download echo-fusion/routemanager
More information about echo-fusion/routemanager
Files in echo-fusion/routemanager
Package routemanager
Short Description A lightweight and flexible routing package that helps manage HTTP routes in PHP applications. Supports PSR-7 and PSR-15, designed for MVC architecture with middleware integration.
License MIT
Homepage https://github.com/echo-fusion/routemanager
Informations about the package routemanager
RouteManager
A lightweight and flexible routing package that helps manage HTTP routes in PHP applications. It supports PSR-7 and PSR-15 standards and is designed for MVC architecture with middleware integration.
Part of EchoFusion, a modular PHP framework built from small packages.
Part of EchoFusion, a modular PHP framework built from small packages.
Installation
Requirements
- PHP 8.1, 8.2 or 8.3
Usage
Basic Setup
-
To get started, you can use the RouteMatcher and Router classes directly.
- Defining Routes Define routes with optional regex constraints on route parameters. The action parameter can be:
- An array (e.g., [Controller::class, 'method'])
- A closure
- Dispatching and Executing a Route Once routes are defined, dispatch the request and execute the action if a match is found.
Path patterns
- A placeholder is
{name}or{name:regex}. Aconstraintsentry for the same name wins over the inline regex. Without either, a parameter matches[a-zA-Z0-9_-]+. - Everything outside the placeholders is literal: in
/file.jsonthe dot is a real dot. - Parameters are returned by name (
$routeMatch->getParams()), even when a constraint has capturing groups of its own. - A path that cannot be compiled (unclosed
{, duplicate or invalid parameter name, broken regex) throwsExceptions\InvalidRouteExceptionwhen the route is registered, not on the first request. - Methods are compared case-insensitively. A
HEADrequest is served by theGETroute of the same path. - One trailing slash on the request is tolerated: a route
/aboutalso matches/about/. A route defined with a trailing slash (/docs/) keeps it. - When nothing matches,
dispatch()throwsRouteNotFoundExceptionwhose message says what was requested. When the path exists but not for the request's method, it throwsMethodNotAllowedException(a subclass, so existingcatch (RouteNotFoundException)code keeps working). Use it to answer405with anAllowheader:
Routes are values
A Route is created complete and never changes (new Route($name, $method, $path, $action, $constraints, $middlewares)); withMiddlewares() returns a changed copy. You normally do not create routes yourself: the Router does when you call get(), post() ..., or fromArray().
Registering versus dispatching
The Router implements two small contracts, so code depends only on what it uses:
EchoFusion\Contracts\RouteManager\RouteRegistrarInterface:get(),post(),put(),patch(),delete(),options()(the code that defines routes, e.g. a route file).EchoFusion\Contracts\RouteManager\RouterInterface:dispatch(),getRoute(),getRoutes()(the code that handles requests).
Using Provider
The RouteManagerProvider allows you to integrate the Router and RouteMatcher instances into a dependency injection container.
-
Register the Provider: Add RouteManagerProvider to your service container.
-
Define Routes Using Configuration: Configure routes by calling the boot method, passing an array of routes or a configuration file. Only
method,pathandactionare required;constraintsandmiddlewaresare optional.If no routes are provided, the boot method defaults to loading routes from routemanager.config.php.
- Dispatching a Route: With the routes registered, retrieve the RouterInterface from the container and dispatch it with a request object.
Testing
Credits
License
The MIT License (MIT). Please see LICENSE for more information.
All versions of routemanager with dependencies
psr/http-message Version ^2.0
echo-fusion/contracts Version ^2.0