A independent, easy-to-use, expandable and simple to understand php web application url routing library, mybe augment your development toolkit or provided a bit inspiration for you.
Download the source code and place right location of your project directory.
Get Router Object Instance
use RouterOneRouter;
$router = Router::getInstance();Create route map in single file and located in specific dir of your prject, in the file $this refer to concrete $router object instance. For the http request verbs, RouteOne only support GET, POST(just align with PHP's $_GET & $_POST, totally extending if you want or necessary.)
For example, Foo.php. RouterOne can support multiple route map files, such as frontend_route_map.php, backend_route_map.php, .
// Closure function call
$this->get('/', function () {
echo 'Hello! RouterOne';
});
// `WelcomeController::hello()` call
$this->get('/', [WeclomeController::class, 'hello']);
Set route map file directory path & loading it. (The route map file default extension is .php)
$router->setIncludePath(`YOUR_ROUTE_MAP_FILE_DIR`);
$router->load(['Foo']); // Just file's name without extensionOr call like this
$router->setIncludePath(`YOUR_ROUTE_MAP_FILE_DIR`)->load(['Foo']); // Just file's name without extensionRun dispatch and enable all loaded routes, it will be return the result about actually route called.
$res = $router->dispatch();Controller Route Action:
// Http GET
$this->get('index', [ControllersSiteController::class, 'index']);
// Http POST
$this->post('news/add', [ControllersNewsController::class, 'add']);Closure Route Action:
// Http GET
$this->get('index', function () {
/**
* Some logic process code here
*/
});
// Http POST
$this->post('news/add', function () {
/**
* Some logic process code here
*/
});With Route Parameters, dynamic paramters with {} wrapped, then will be transfer to controller method or clousre function paramter by the order of appearance.
$this->get('test/{param1}/{param2}', [ControllersTestController::class, 'params']);
class TestController
{
public function params($param1, $param2)
{
// Some code ...
}
}$this->get('test/{param1}/{param2}', function ($param1, $param2) {
// Some code
});MiddleWare should be a implementation to RouteMiddleWareInterface, you can locate middle-ware class file in arbitrary directory, such as MiddleWare dir;
A typical middle-ware class contain a handle() method with route action-$action parameter, like below:
use RouterOneMiddleWareRouteMiddleWareInterface;
class AuthCheckMiddleWare implements RouteMiddleWareInterface
{
public static function handle($action)
{
if ( ! AdminUser::Logged) {
exit('Please login first.');
}
$action();
}
}In some cases you may want do some process after route action excuted, just place middle-ware logic behind $action() call statement.
use RouterOneMiddleWareRouteMiddleWareInterface;
class AfterMiddleWare implements RouteMiddleWareInterface
{
public static function handle($action)
{
$action();
echo 'This text will print after route action excuted.';
}
}When defined middle-ware, and can through router's middleware() method setting routes as grouped form, middleware() has two parameters, the first is a middle-ware class name array and support more middle-wares here, and the other is a closure function include common route mapping.
$this->middleware(
[
AuthCheckMiddleWare::class,
...
...
], function () {
$this->get('admin/index', [ControllersAdminAdminController::class, 'index']);
$this->get('admin/news/list', [ControllersAdminNewsController::class, 'list']);
...
...
});Also can nested
$this->middleware(
[
OuterMiddleWare::class,
], function () {
...
$this->middleware([
InnerMiddleWare::class
], function () {
$this->get(...
$this->post(...
...
});
...
...
});prefix() and suffix() method are grouped routes too, they can convenient add practical prefix and suffix to specific routes.
Add the prefix static/, then urls '://domain/static/page1', '//domain/static/page2' will be matched.
$this->prefix('static/', function () {
$this->get('page1', ...);
$this->get('page2', ...);
...
});Add the preifx the, then urls '://domain/thepage1', '//domain/thepage2' will be matched.
$this->prefix('the', function () {
$this->get('page1', ...);
$this->get('page2', ...);
...
});As same as prefix() using, add the suffix .html, then url change to '://domain/page1.html'.
$this->suffix('.html', function () {
$this->get('page1', ...);
...
...
});Between prefix() and suffix() can nested each other.
$this->prefix('static/', function () {
$this->get('page1', ...); // request url '://domain/static/page1' matched here
...
$this->suffix('.html', function () {
$this->get('page2', ...); // request url '://domain/static/page2.html' matched here
});
});If your application has multiple url domain, use domain() method can distinguish these domain and guiding the request to corresponding route group.
$this->domain('www.hereisyoursite.com', function () { // PC Pages
$this->get('index', ...);
...
});
$this->domain('m.hereisyoursite.com', function () { // Mobile Pages
$this->get('index', ...);
...
});
$this->domain('new.hereisyoursite.com', function () { // Current Domain routes
$this->get('index', ...);
...
});
$this->domain('old.hereisyoursite.com', function () { // Legacy Domain routes
$this->get('index', ...);
...
});