laudirbispo / easy-route
简单的路由类
1.0.1
2019-02-26 15:42 UTC
Requires
- laudirbispo/classname: ^1.2.1
This package is auto-updated.
Last update: 2024-09-28 00:42:26 UTC
README
这是一个简单的类,可以帮助你在应用程序中开始处理路由。
安装
使用以下命令安装最新版本
$ composer require laudirbispo/easy-route
基本用法
<?php use laudirbispo\EasyRouter\EasyRouter; use laudirbispo\EasyRouter\Exceptions\RouterException; $router = new EasyRouter(); /** * @param $pattern - string|array routes - accept multiple routes * @param $calback - class with method string|closure */ $router->add($pattern, $callback); //Examples $router->add( ['/admin/login', '/admin/login?(.*)'], 'app\IdentityAccess\Application\Controllers\AccountController::pageLogin' ); // (.*) that is to say that it accepts any parameter after "?" // example - /admin/login?return=other_url // Closures and Regular expressions $router->add(['/admin/users/group/edit/([-\w]{36}$)'], function ($groupId){ $controller = new app\IdentityAccess\Application\Controllers\AdminGroups; $controller->pageEditGroup($groupId); }); // For example, this regular expression, accepts only string in the format "4d428391-8975-4158-b68a-9e3054e3df2c" // "4d428391-8975-4158-b68a-9e3054e3df2c" is uuid4 string // Other example $router->add('news/(\w+)/(\d+)', function($category, $year){ $controller = new app\News\Application\Controllers\News; $controller->getNewsByYear($category, $year); }); // Check if router exists $router->has(string $pattern); // Execute $router->execute($_SERVER['REQUEST_URI']); // Exeptions // RouterException // ControllerDoesNotExists // InvalidRoute // MethodDoesNotExists // RouteNotFound
作者
Laudir Bispo - laudirbispo@outlook.com - https://twitter.com/laudir_bispo
许可证
EasyRouter 在 MIT 许可证下发布 - 详细信息请参阅 LICENSE
文件 自由软件,太棒了!