Download the PHP package willy68/pg-router without Composer
On this page you can find all versions of the php package willy68/pg-router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download willy68/pg-router
More information about willy68/pg-router
Files in willy68/pg-router
Package pg-router
Short Description A fast, flexible, and PSR-7 compatible router for PHP.
License MIT
Informations about the package pg-router
pg-router
A fast, flexible, and PSR-7 compatible HTTP router for PHP applications. Built for performance with support for advanced routing patterns, middleware stacking, and route caching.
Table of Contents
- Features
- Credits
- Requirements
- Installation
- Quick Start
- Advanced Usage
- Route Parameters
- Optional Segments
- Route Groups
- Middleware
- Named Routes
- CRUD Helper
- URL Generation
- Route Caching
- Performance
- Testing
- Contributing
- License
Features
- ✅ PSR-7 Compatible: Full support for PSR-7 request interfaces
- 🚀 High Performance: Optimized route matching with caching support
- 🎯 Flexible Routing: Support for complex route patterns and constraints
- 🔧 Middleware Support: Route-level and group-level middleware stacking
- 📦 Route Grouping: Organize routes with shared prefixes and middlewares
- 🎨 Named Routes: Easy URL generation with named route support
- ⚡ CRUD Helpers: Quick REST resource route generation
- 🔀 Optional Segments: Advanced optional route segment support
- 🏗️ Extensible: Custom matchers and collectors for advanced use cases
Credits
The regex pattern implementation and benchmarking are inspired by nikic/Fast-Route.
For a detailed explanation of how this approach works and why it is fast, please read nikic's blog post.
This router's implementation is original and may differ in speed or structure compared to Fast-Route.
Some parts do not strictly follow the same patterns as nikic/Fast-Route.
Requirements
- PHP 8.1 or higher
- Composer
Installation
Install via Composer:
Quick Start
Advanced Usage
Route Parameters
It is possible to define default tokens for parameters:
Optional Segments
Breaking Change: Optional segments now use a new syntax with [!...;...] pattern:
This route matches:
/article(no parameters)/article/123(id parameter only)/article/123/my-article-title(both id and slug parameters)
Parameters will be available in the request based on the provided segments.
Route Groups
Organize related routes with shared prefixes and middlewares:
Middleware
Apply middleware to individual routes or groups:
Named Routes
Generate URLs using named routes:
CRUD Helper
This creates the following routes:
GET /posts→PostController::index(posts.index)GET /posts/new→PostController::create(posts.create)POST /posts/new→PostController::create(posts.create.post)GET /posts/{id:\d+}→PostController::edit(posts.edit)POST /posts/{id:\d+}→PostController::edit(posts.edit.post)DELETE /posts/{id:\d+}→PostController::delete(posts.delete)
URL Generation
Route Caching
Enable route caching for production environments:
Router::CONFIG_CACHE_POOL_FACTORY allows you to use a custom PSR-6 compatible cache pool implementation,
but this parameter is optional.
Using FileCache with Regex Collector
The FileCache class provides a simple file-based caching solution that works well with all regex collectors.
Here's a practical example of using FileCache with route collectors:
In this example:
- We create a
FileCacheinstance pointing to a cache directory - We set up all available Collectors for route collection
- We use
fetch()which will return cached data if available, or execute the callback to generate and cache the data - We measure and display the execution time to demonstrate the performance benefit of caching
The fetch() method is particularly useful as it handles both the cache check and the callback execution in one call, making your code cleaner and more efficient.
Using the returned data directly in a Matcher like:
Performance
pg-router is designed for high performance:
- Optimized Route Matching: Uses efficient algorithms for route compilation and matching
- Route Caching: Cache compiled routes for production use
- Minimal Memory Footprint: Efficient memory usage for large route tables
- Fast Parameter Extraction: Optimized parameter extraction from matched routes
Testing
This project uses PHPUnit for testing. To run the tests, ensure you have PHPUnit installed via Composer:
Contributing
Contributions are welcome! Please follow these guidelines:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Please ensure your code follows PSR-12 coding standards and includes appropriate tests.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Author: William Lety Maintainer: willy68 Repository: pg-router
All versions of pg-router with dependencies
psr/container Version ^1.0 || ^2.0
psr/http-message Version ^1.0
fig/http-message-util Version ^1.1
psr/http-server-middleware Version ^1.0
willy68/pg-middleware-stack Version ^0.1
symfony/cache Version ^7.2
psr/cache Version ^3.0
psr/simple-cache Version ^3.0