Download the PHP package phputil/router without Composer
On this page you can find all versions of the php package phputil/router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package router
phputil/router
ExpressJS-like router for PHP
- No third-party dependencies
- Unit-tested
- Mockable - it's easy to create automated tests for your API
👉 Do NOT use it in production yet - just for toy projects.
Installation
Requires PHP 7.4+
👉 You may also like to install phputil/cors.
Notes
- Unlike ExpressJS,
phputil/router
needs an HTTP server to run (if the request is not http-server.- See Server Configuration for more information.
- If you are using Apache or Nginx, you may need to inform the
rootURL
parameter when callinglisten()
. Example:
Examples
Hello World
Using parameters
Middleware per route
ℹ To help us with an example, just submit a Pull Request or open an Issue with the code.
Features
- [✔] Support to standard HTTP methods (
GET
,POST
,PUT
,DELETE
,HEAD
,OPTIONS
) andPATCH
. - [✔] Route parameters
- e.g.
$app->get('/customers/:id', function( $req, $res ) { $res->send( $req->param('id') ); } );
- e.g.
- [✔] URL groups
- e.g.
$app->route('/customers/:id')->get('/emails', $cbGetEmails );
- e.g.
- [✔] Global middlewares
- e.g.
$app->use( function( $req, $res, &$stop ) { /*...*/ } );
- e.g.
- [✔] Middlewares per URL group
- e.g.
$app->route( '/admin' )->use( $middlewareIsAdmin )->get( '/', function( $req, $res ) { /*...*/ } );
- e.g.
- [✔] Middlewares per route
- e.g.
$app->get( '/', $middleware1, $middleware2, function( $req, $res ) { /*...*/ } );
- e.g.
- [✔] Request cookies
- e.g.
$app->get('/', function( $req, $res ) { $res->send( $req->cookie('sid') ); } );
- e.g.
- [✔] Extra: Can mock HTTP requests for testing, without the need to running an HTTP server.
- [🕑] (soon) Deal with
multipart/form-data
onPUT
andPATCH
Known Middlewares
- phputil/cors - CORS Middleware
- phputil/csrf - Anti CSRF Middleware
ℹ Did you create a useful middleware? Open an Issue for including it here.
API
This library does not aim to cover the entire ExpressJS API. However, feel free to contribute to this project and add more features.
Types:
- Middleware
- Router
- RouterOptions
- HttpRequest
- ExtraData
- HttpResponse
Middleware
In phputil/router
, a middleware is a function that:
- Perform some action (e.g., set response headers, verify permissions) before a route is evaluated.
- Can stop the router, optionally setting a response.
Syntax:
where:
$req
allows to get all the request headers and data.$res
allows to set all the response headers and data.$stop
allows to stop the router, when set totrue
.
Router
Class that represents a router.
get
Method that deals with a GET
HTTP request.
where:
$route
is a route (path).$callbacks
can receive none, one or more middleware functions and one route handler - which must be the last function.
A route handler has the following syntax:
where:
$req
allows to get all the request headers and data.$res
allows to set all the response headers and data.
Examples:
post
Method that deals with a POST
HTTP request. Same syntax as get's.
put
Method that deals with a PUT
HTTP request. Same syntax as get's.
delete
Method that deals with a DELETE
HTTP request. Same syntax as get's.
head
Method that deals with a HEAD
HTTP request. Same syntax as get's.
option
Method that deals with a OPTION
HTTP request. Same syntax as get's.
patch
Method that deals with a PATCH
HTTP request. Same syntax as get's.
all
Method that deals with any HTTP request. Same syntax as get's.
group
Alias to the method route.
route
Method that adds a route group, where you can register one or more HTTP method handlers.
Example:
end
Method that finishes a route group and returns to the group parent.
Example:
use
Method that adds a middleware to be evaluated before the routes declared after it.
Example:
listen
Method that executes the router.
Options are:
rootURL
is a string that sets the root URL. Example:dirname( $_SERVER['PHP_SELF'] )
. By default it is''
.req
is an object that implements the interfaceHttpRequest
, which retrieves all the headers and data from a HTTP request. Changing it is only useful if you want to unit test your API - see Mocking an HTTP request. By default, it will receive an object from the classRealHttpRequest
.res
is an object that implements the interfaceHttpResponse
. You probably won't need to change its value. By default, it will receive an object from the classRealHttpResponse
.
Example:
You can also use an instance of RouterOptions
for setting the options:
RouterOptions
Options for the listen() method.
withRootURL
withReq
withRes
HttpRequest
Interface that represents an HTTP request.
API:
ExtraData
Extra, user-defined data.
HttpResponse
Interface that represents an HTTP response.
Most of its methods are chainable, that is, you can call them in a sequence. Example:
API:
Mocking an HTTP request
👉 Useful for API testing
License
Thiago Delgado Pinto
All versions of router with dependencies
ext-mbstring Version *
ext-json Version *
ext-pcre Version *