Download the PHP package xiaozhuangyuan/srouter without Composer
On this page you can find all versions of the php package xiaozhuangyuan/srouter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download xiaozhuangyuan/srouter
More information about xiaozhuangyuan/srouter
Files in xiaozhuangyuan/srouter
Package srouter
Short Description Simple PHP router class.
License MIT
Homepage https://github.com/xiaozhuangyuan/Srouter.git
Informations about the package srouter
Srouter
Srouter is a simple, open source PHP router. It's super small (~150 LOC), fast, and has some great annotated source code. This class allows you to just throw it into your project and start using it immediately.
Install
If you have Composer, just include Srouter as a project dependency in your composer.json
. If you don't just install it by downloading the .ZIP file and extracting it to your project directory.
Examples
First, use
the Srouter namespace:
Srouter is not an object, so you can just make direct operations to the class. Here's the Hello World:
Srouter also supports lambda URIs, such as:
You can also make requests for HTTP methods in Srouter, so you could also do:
Lastly, if there is no route defined for a certain location, you can make Srouter run a custom callback, like:
If you don't specify an error callback, Srouter will just echo 404
.
In order to let the server know the URI does not point to a real file, you may need to use one of the example configuration files.
Example passing to a controller instead of a closure
It's possible to pass the namespace path to a controller instead of the closure:
For this demo lets say I have a folder called controllers with a Test.php
index.php:
Test.php:
Advanced Usage
index.php:
Test.php:
Middleware
Auth.php:
This is with Srouter installed via composer.
composer.json:
`
.htaccess(Apache):
Nginx: