Wallogit.com
2017 © Pedro Peláez
Simple PHP router class base on Macaw
Router is a simple, open source PHP router base on Macaw. 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., (*1)
If you have Composer, just include Router 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., (*2)
require: {
"hongjh/router": "dev-master"
}
First, use the Router namespace:, (*3)
use \Hongjh\Router\Router;
Router is not an object, so you can just make direct operations to the class. Here's the Hello World:, (*4)
Router::get('/', function() {
echo 'Hello world!';
});
Router::dispatch();
Router also supports lambda URIs, such as:, (*5)
Router::get('/(:any)', function($slug) {
echo 'The slug is: ' . $slug;
});
Router::dispatch();
You can also make requests for HTTP methods in Router, so you could also do:, (*6)
Router::get('/', function() {
echo 'I <3 GET commands!';
});
Router::post('/', function() {
echo 'I <3 POST commands!';
});
Router::dispatch();
Lastly, if there is no route defined for a certain location, you can make Router run a custom callback, like:, (*7)
Router::error(function() {
echo '404 :: Not Found';
});
If you don't specify an error callback, Router will just echo 404., (*8)
, (*9)
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., (*10)