sirix / mezzio-radixrouter
RadixRouter integration for Mezzio
Requires
- php: ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0
- fig/http-message-util: ^1.1.2
- laminas/laminas-stdlib: ^3.19.0
- mezzio/mezzio-router: ^3.14 || ^4.0.1
- psr/container: ^1.0 || ^2.0
- psr/http-message: ^1.0.1 || ^2.0.0
- wilaak/radix-router: ^1.1
Requires (Dev)
- ergebnis/composer-normalize: ^2.47
- laminas/laminas-diactoros: ^3.5.0
- laminas/laminas-stratigility: ^4.1.0
- mikey179/vfsstream: ^1.6.12
- phpbench/phpbench: ^1.2
- phpunit/phpunit: ^12
README
RadixRouter integration for Mezzio, providing high-performance HTTP routing using a radix tree algorithm.
Installation
Install this package via Composer:
composer require sirix/mezzio-radixrouter
Features
- High-performance routing using a radix tree algorithm
- PSR-7 and PSR-15 compatible
- Supports route parameters and optional parameters
- Route caching for improved performance
- Fully compatible with Mezzio middleware architecture
Usage
Basic Setup
There are two ways to set up RadixRouter in your Mezzio application:
Automatic Configuration (Recommended)
The easiest way to set up RadixRouter is to use the provided ConfigProvider, which automatically registers all necessary dependencies:
// In config/config.php or similar configuration file $aggregator = new ConfigAggregator([ // ... other config providers \Sirix\Mezzio\Router\RadixRouter\ConfigProvider::class, // ... other config providers ]);
This will automatically register the RadixRouter as the default router implementation for your application.
Manual Configuration
Alternatively, you can manually update your dependencies configuration:
// In config/autoload/dependencies.php or similar configuration file use Mezzio\Router\RouterInterface; use Sirix\Mezzio\Router\RadixRouter; use Sirix\Mezzio\Router\RadixRouterFactory; return [ 'dependencies' => [ 'factories' => [ RouterInterface::class => RadixRouterFactory::class, ], ], ];
Route Configuration
Routes can be defined in your Mezzio application as usual:
// In config/routes.php or similar $app->get('/api/users', [UserListHandler::class], 'api.users'); $app->get('/api/users/:id', [UserDetailsHandler::class], 'api.user'); $app->post('/api/users', [CreateUserHandler::class]);
Caching Configuration
To enable route caching for improved performance:
// In config/autoload/router.global.php or similar use Sirix\Mezzio\Router\Enum\CacheConfig; return [ 'router' => [ 'radix' => [ CacheConfig::Enabled->value => true, CacheConfig::File->value => 'data/cache/radix-cache.php', ], ] ];
Documentation
For more information about routing in Mezzio, please refer to the Mezzio routing documentation.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Credits
- Sirix - Project maintainer
- Wilaak RadixRouter - The underlying radix tree router implementation
- Mezzio - The middleware framework