Download the PHP package softwarepunt/minirouter without Composer
On this page you can find all versions of the php package softwarepunt/minirouter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download softwarepunt/minirouter
More information about softwarepunt/minirouter
Files in softwarepunt/minirouter
Package minirouter
Short Description Fast and lightweight PSR-7 request router for PHP
License MIT
Informations about the package minirouter
MiniRouter
A fast and lightweight router for PHP, compatible with PSR-7:
- ⏩ Super fast request-response routing
- 🌟 Static or dynamic routes with unlimited URL variables
- 💉 Automatic parameter injection for requests and route variables
Installation
Install the package using Composer:
This package is compatible with PHP 8.2+.
Usage
Registering routes
Initialize a new instance of MiniRouter
and start registering routes:
Incoming requests (PSR-7)
You will need a PSR-7 implementation, such as guzzlehttp/psr7
, to provide incoming request data. For example:
Dispatching requests
Once your routes are registered, pass your request object (any PSR-7 compatible RequestInterface
) to the router:
This call will return a PSR-7 compatible ResponseInterface
.
If a matching route is found, your target function will be executed and its response will be returned. It can return its own response object, or a string.
Accessing the request
When your target function is called, you can ask for an instance of RequestInterface
to access the request directly:
The request object will be injected automatically. The name and order of the parameter doesn't matter.
Routes with variables
When you register your routes, you can also use one or more URL variables that can then be injected into your target function:
Variables are defined in the route by using the $
prefix. Their values are automatically extracted from the request URL, and injected into your target function as named parameters (strings).
Routing to (controller) classes
You can also register routes that will construct a class instance, and invoke a specific method. This can help you organize your code, and is more typical for a model-view-controller (MVC) architecture.
You can use the optional before
method to perform pre-flight checks and run common code - for example, to handle authentication before allowing routing to proceed.
Simple redirects
You can also use the registerRedirect
utility function to quickly register HTTP 301 or 302 redirects:
Examples
Complete index.php
All versions of minirouter with dependencies
psr/http-message Version *
technically/callable-reflection Version ^0.4.0