Download the PHP package puz/router without Composer
On this page you can find all versions of the php package puz/router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package router
Puz Router
The most fluffiest PHP router you have ever used
Installation
To use it with composer, simply type composer require puz/router
or add the following to your composer.json
file.
If you want to use the latest unreleased version, you need to require the dev version, like this:
How-To
Create a router
Easy as:
The router is the object which contains different validation rules on how to validate the url. By default there are two validation rules added.
\Puz\Router\Validations\MethodValidator
and \Puz\Router\Validations\UrlValidator
. These will validate routes against the request method and the given url.
You can modify this list like this:
Create a route
This is done through the Router object. Per now i have added six different request methods. GET
, HEAD
, POST
, PUT
, PATCH
, DELETE
. To create a route with one of these methods:
If you chain two request method methods, the last one will overwrite the previous. To allow multiple request methods you have to use the method
method.
To provide the url you can (if you did not specify it in the method methods) use the url
method.
To provide the callback you can (if you did not specify it in the method methods or the url method) use the callback
method.
For now the router only accepts callable
callbacks. (And pure strings that is just for echoing, ex: ->callback("ping") results in ping being echoed if it's not a valid function).
Run the router
To run the router you have to end your application with $router->run(...)
.
This router will not auto-detect your request method nor requested uri, so you have to fill in the blanks. Heres an easy example of how to do that.
This also opens up for a easy way to debug or do it just the way you want it!
If you are using this router in a subfolder on your domain, you might have a bad time with the router uri if you're using this exact run example. You will need to filter away the subfolder from the request uri before giving it to the router. As for now i use this:
Examples
I will update the examples some other time, but not too far in the future. I hope this readme will provide enough information on how you can use the router.
Planned changes
[ ] Adding response converters. If your callback returns data, these converters will convert your response to ex. json output and such.
Got some ideas? Please give me a pling in the issue section!