Download the PHP package 8ctopus/nano-router without Composer
On this page you can find all versions of the php package 8ctopus/nano-router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download 8ctopus/nano-router
More information about 8ctopus/nano-router
Files in 8ctopus/nano-router
Package nano-router
Short Description An experimental PSR-7, PSR-17 router
License MIT
Homepage https://github.com/8ctopus/nano-router
Informations about the package nano-router
nano router
An experimental PSR-7, PSR-17 router
features
- very fast (less than 2ms on simple routing)
- uses PSR-7 and PSR-17 standards
- no dependencies besides PSR-7/17
While I consider it still experimental, I have been using it in production to host legend.octopuslabs.io without any issues so far.
introduction for beginners
The purpose of a router is to match a user (client) http request to a specific function that will handle the user request and deliver a response to the client.
PSR-7 defines the request and response interfaces, while PSR-17 defines the factories for creating them. In other words, factories are used to create the request and response objects.
Here's some pseudo-code that explains the concept:
demo
To play with the demo, clone the repo, run php -S localhost:80 demo/public/index.php -t demo/public/
and open your browser at http://localhost
.
Alternatively you can run the demo within a Docker container docker-compose up &
.
install
-
composer require 8ctopus/nano-router
-
if you don't have any preference for the PSR-7 implementation, install HttpSoft
composer require httpsoft/http-message httpsoft/http-emitter
- redirect all traffic (except existing files) to the router in
.htaccess
for those using Apache
and for nginx (untested)
- create
index.php
exception handling
By default, Throwable
, and everything inherited from it such as Exception
, are caught by the router provided the exception occurs within the route code. That functionality can be disabled in the constructor by setting it to false
.
The RouteException
class offers an elegant way to deal with http errors such as 404
, 401
, 429
, .... The router will automatically catch the exception and return the appropriate response to the client.
advanced functionalities
There is more to it, it's just not in the readme yet, most of it can be experimented within the demo, such as:
- pre and post middleware
run tests
composer test
clean code
composer fix(-risky)
todo ideas
- add basePath
- class wrapper for subroutes
- should pre middleware only work on valid requests? now not valid routes are still going through the middleware probably we need both
- add starts with middleware
- check psr-15 middleware
- how to easily route inside class?