Download the PHP package devcoder-xyz/php-router without Composer
On this page you can find all versions of the php package devcoder-xyz/php-router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download devcoder-xyz/php-router
More information about devcoder-xyz/php-router
Files in devcoder-xyz/php-router
Package php-router
Short Description A versatile and efficient PHP routing solution designed to streamline route management within PHP applications.
License MIT
Informations about the package php-router
PHP Router
PHP Router is a simple and efficient routing library designed for PHP applications. It provides a straightforward way to define routes, handle HTTP requests, and generate URLs. Built with PSR-7 message implementation in mind, it seamlessly integrates with PHP applications.
Installation
You can install PHP Router via Composer. Just run:
Composer Require
Requirements
- PHP version 7.4 or above
- Enable URL rewriting on your web server
- Optional: PSR-7 HTTP Message package (e.g., guzzlehttp/psr7)
Usage
-
Define Routes: Define routes using the
Route
class provided by PHP Router. -
Initialize Router: Initialize the
Router
class with the defined routes. -
Match Requests: Match incoming HTTP requests to defined routes.
-
Handle Requests: Handle matched routes by executing appropriate controllers or handlers.
- Generate URLs: Generate URLs for named routes.
Example
Features
- Lightweight and easy-to-use
- Supports HTTP method-based routing
- Flexible route definition with attribute constraints
- Exception handling for method not allowed and route not found scenarios
Route Definition
Routes can be defined using the Route
class provided by PHP Router. You can specify HTTP methods, attribute constraints, and handler methods for each route.
Easier Route Definition with Static Methods
To make route definition even simpler and more intuitive, the RouteTrait
provides static methods for creating different types of HTTP routes. Here's how to use them:
Method get()
Example Usage:
Method post()
Example Usage:
Method put()
Example Usage:
Method delete()
Example Usage:
With these static methods, defining routes becomes a breeze, providing a smoother and more efficient way to handle routing in your PHP application.
Using where
Constraints in the Route Object
The Route
object allows you to define constraints on route parameters using the where
methods. These constraints validate and filter parameter values based on regular expressions. Here's how to use them:
Method whereNumber()
This method applies a numeric constraint to the specified route parameters.
Example Usage:
Method whereSlug()
This method applies a slug constraint to the specified route parameters, allowing alphanumeric characters and hyphens.
Example Usage:
Method whereAlphaNumeric()
This method applies an alphanumeric constraint to the specified route parameters.
Example Usage:
Method whereAlpha()
This method applies an alphabetic constraint to the specified route parameters.
Example Usage:
Method where()
This method allows you to define a custom constraint on a specified route parameter.
Example Usage:
By using these where
methods, you can apply precise constraints on your route parameters, ensuring proper validation of input values.
Generating URLs
Generate URLs for named routes using the generateUri
method.
Contributing
Contributions are welcome! Feel free to open issues or submit pull requests to help improve the library.
License
This library is open-source software licensed under the MIT license.
All versions of php-router with dependencies
psr/http-message Version ^1.0|^2.0
psr/http-server-middleware Version ^1.0
psr/http-factory Version ^1.0