Download the PHP package marcosdipaolo/router without Composer
On this page you can find all versions of the php package marcosdipaolo/router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download marcosdipaolo/router
More information about marcosdipaolo/router
Files in marcosdipaolo/router
Informations about the package router
MDP Router
A modern, production-ready PHP router library built with PHP 8.2+ and attribute-based routing. It provides a clean, expressive API for defining routes, handling middleware, and managing HTTP requests and responses.
Features
- โจ Attribute-Based Routing: Define routes using modern PHP 8 attributes
- ๐ฏ Dynamic Route Parameters: Support for path parameters like
/users/{id} - ๐ Middleware Pipeline: Built-in middleware support with global and route-specific middleware
- โก Route Caching: Cache compiled routes for optimal performance
- ๐ Full HTTP Support: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS
- ๐ Route Groups: Organize related routes under common prefixes and middleware
- ๐ Reverse Routing: Generate URLs from route names and parameters
- ๐จ Method Spoofing: Support for
_methodparameter to simulate HTTP verbs in forms - ๐ก๏ธ Security Headers: Built-in CORS and security header support
- ๐ Request/Response Objects: Clean abstractions for HTTP requests and responses
- ๐งช Comprehensive Testing: Full test suite for all features
Installation
Quick Start
1. Setup Your Entry Point
Create an index.php file at your project root:
2. Configure Your .htaccess
3. Define Routes in Controllers
Core Concepts
Request/Response Objects
The router provides clean abstractions for HTTP requests and responses:
Route Parameters
Define dynamic routes with parameters:
Route Groups
Group related routes with common prefixes and middleware:
Middleware
Create custom middleware by implementing MiddlewareInterface:
Add middleware globally or per-route:
Built-in Middleware
CORS Middleware
Handle cross-origin requests:
Security Headers Middleware
Add security headers automatically:
Method Spoofing Middleware
Allow forms to use PUT/PATCH/DELETE via _method parameter:
In your HTML form:
Route Caching
Routes are automatically cached when enabled. This compiles all route definitions into a PHP file for maximum performance:
Reverse Routing
Generate URLs from route names:
Debugging
Enable debug mode to get detailed error information:
HTTP Status Codes
Commonly used status codes:
Security Best Practices
- Always validate input: Validate and sanitize all user input
- Use HTTPS: Ensure all traffic is encrypted
- Add CORS headers: Only allow trusted origins
- Implement authentication: Protect sensitive routes with middleware
- Use prepared statements: When working with databases
- Keep cache directory secret: Don't expose the cache directory publicly
- Enable security headers: Always include security headers
Error Handling
The router provides built-in error handling with debug mode for development:
Create custom error handling middleware:
Testing
The router includes comprehensive tests:
Write your own tests:
Performance Tips
- Enable route caching (production)
- Use parameter constraints in routes
- Load only needed controllers in Router::create()
- Cache middleware results when possible
- Use lazy loading for dependencies in container
Contributing
Contributions are welcome! Please read CONTRIBUTING.md first.
License
MIT License - see LICENSE file