Download the PHP package math280h/php-router without Composer
On this page you can find all versions of the php package math280h/php-router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package php-router
php-router
Simple PHP Router, it supports features such as middlewares and views using aura/view
Installation
Usage
For simple usage you can inline callback functions directly in the routes as shown below.
Note: The router always passes the Request object to callback functions
Accepting different HTTP Methods
The router exposes a function for each accepted HTTP method. The list of available methods are:
- GET
- POST
- PUT
- DELETE
- OPTIONS
- HEAD
- PATCH
And they can be called like so:
Route groups
You can group routes together, this allows you to e.g. define a prefix for a colleciton of routes or apply middleware to the collection of routes.
You can define a route group like so:
This example results in two routes with the paths /test/1
and /test/2
Adding middleware
The router supports middleware than runs after the connection is accepted but before the request is forwarded to the callback function.
For now, only direct callback functions are supported and can be added to the router like so:
The router will always pass the request object to the callback function.
Once the middleware is added to the router you can attach it to your route like so:
Passing callbacks from classes to the router
The router allows you to pass references to functions inside classes instead of inlining the callback functions You can do this like so:
Returning views
The router is built with support for aura/view. This allows callback functions to pass back a view and the router will automatically render it.
This can achieved like so:
It's recommended to implement a helper function for spinning up new views so you don't have to duplicate the factory creation in your code
View helper function
A view helper function can look something like this: