denosyscore / routing
DenosysCore: Fast PSR-7 Router and Dispatcher PHP package with PSR-15 Middleware support
v0.1.0
2024-05-27 15:57 UTC
Requires
- php: ^8.1
- psr/container: ^2.0
- psr/http-factory: ^1.1
- psr/http-message: ^2.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- laminas/laminas-diactoros: ^3.3
- laminas/laminas-httphandlerrunner: ^2.10
- php-di/php-di: ^7.0
- symfony/var-dumper: ^7.0
README
A highly efficient and flexible routing package for PHP, designed to support modern web applications with minimal overhead.
Features
- Fast Route Matching: Utilizes a trie data structure for efficient route matching.
- Flexible Handlers: Supports various types of handlers (closures, arrays, strings).
- PSR-7 Compliant: Compatible with PSR-7 HTTP message interfaces.
- PSR-15 Middleware Support: Allows middleware to be attached to routes.
- Customizable Invocation Strategies: Define how route handlers are invoked.
- Dependency Injection: Integrates with PSR-11 containers for automatic dependency resolution.
- Dynamic and Static Routes: Easily define and handle both dynamic and static routes.
Requirements
- PHP 8.1 or later
Usage
Install the package using Composer:
composer require denosyscore/routing
Install a PSR-7 implementation, such as Laminas Diactoros:
composer require laminas/laminas-diactoros
Here's a simple example of how to define routes and handle requests:
// Create a new router instance $router = new Denosys\Routing\Router(); // Define a route $router->get('/', function (): ResponseInterface { $response = new Laminas\Diactoros\Response(); $response->getBody()->write('Hello, World!'); return $response; }); // Create Request $request = Laminas\Diactoros\ServerRequestFactory::fromGlobals(); // Dispatch the request $response = $router->dispatch($request); // Output the response echo $response->getBody();
Adding Routes
You can add routes using various HTTP methods:
$router->get('/user/{id}', 'UserController@show'); $router->post('/user', 'UserController@store'); $router->put('/user/{id}', 'UserController@update'); $router->delete('/user/{id}', 'UserController@destroy'); $router->patch('/user/{id}', 'UserController@patch'); $router->options('/user', 'UserController@options'); $router->any('/any-method', 'AnyController@handle');
Full documentation coming soon...
Contributing
Please see CONTRIBUTING for details.
License
The MIT License (MIT). Please see License File for more information.