easyroute / easyroute
PHP 的快速、功能齐全的 Restful 请求路由器
v1.0.1
2016-04-13 11:21 UTC
Requires
- php: >=5.5.0
Requires (Dev)
Suggests
- symfony/http-foundation: Use Symfony Request components
- symfony/psr-http-message-bridge: Convert Symfony Request to Psr7
- zendframework/zend-diactoros: Psr7 response and request
This package is not auto-updated.
Last update: 2024-09-26 00:57:25 UTC
README
一个简单且极其灵活的 PHP 路由类,支持路由参数、Restful、过滤器及反向路由。
入门指南
使用 EasyRoute 需要 PHP >= 5.5。
安装
系统要求
要使用 EasyRoute\EasyRoute,你需要 PHP >= 5.5.0,但推荐使用 PHP 的最新稳定版本。
Composer
EasyRoute 可在 Packagist 上找到,并可以使用 Composer 进行安装。
composer require easyroute/easyroute
手动安装
只要你的自动加载器遵循 PSR-0 或 PSR-4 标准,你就可以使用自己的自动加载器。只需将 src 目录的内容放入你的 vendor 目录中。
重写请求
要使用 EasyRoute,你需要将所有请求重写到单个文件。有各种方法可以做到这一点,但以下是 Apache 和 Nginx 的示例。
Apache .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ index.php [NC,L]
Nginx nginx.conf
server { listen 80; server_name mydevsite.dev; root /var/www/mydevsite/public; index index.php; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini # With php5-fpm: fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi.conf; fastcgi_intercept_errors on; } }
映射你的路由
use EasyRoute\Router; $router = new Router('/examples'); $router->filter('auth', function ($_requesturi) { //return 'hola auth'; var_dump($_requesturi); }); $router->get('/', function () { return 'hello world'; }); $router->get('/part/', function () { return 'hello world part'; })->setName('part'); $router->get('/part/{id:[0-9]+}/', function ($id) { return 'hello world part' . $id; })->setName('partid'); $router->group(['before' => ['auth']], function (\EasyRoute\Router $router) { $router->get('/testbefore/', function () { return 'hello test before'; }); }); $router->group(['prefix' => 'en'], function (\EasyRoute\Router $router) { $router->get('/', function () { return 'home with prefix en'; })->setName('home'); }); $router->group(['prefix' => 'es'], function (\EasyRoute\Router $router) { $router->get('/', function () { return 'home with prefix es'; })->setName('home'); $router->group(['prefix' => 'admin'], function (Router $router) { $router->get('/', function () { return 'hola 2 prefix'; })->setName('prefix2'); }); }); $router->get('/home/', function () { return 'home without prefix'; })->setName('home');
匹配请求
$data = $router->getData(); $dispatcher = new \EasyRoute\Dispatcher($data); $request = \Symfony\Component\HttpFoundation\Request::createFromGlobals(); try { echo $dispatcher->dispatchRequest($request->getMethod(), $request->getUri()); } catch (\EasyRoute\Exception\HttpRouteNotFoundException $e) { echo 'route not found'; } catch (\EasyRoute\Exception\HttpMethodNotAllowedException $e) { echo 'not method allowed'; } catch (\Exception $e) { echo 'error 500'; }
分发 URL
$data = $router->getData(); $dispatcher = new \EasyRoute\Dispatcher($data); $request = \Symfony\Component\HttpFoundation\Request::createFromGlobals(); echo $dispatcher->getUrlRequest('home', [], $request->getUri()); echo "\n"; echo $dispatcher->getUrlRequest('prefix2', [], $request->getUri()); echo "\n"; echo $dispatcher->getUrlRequest('partid', ['id' => 555], $request->getUri(); echo "\n";
待办事项
- add requestinterface optional (guzzle / psr7)
- default pregmatches
- add cache route collection
- add cache dispatcher
许可证
MIT 许可证 (MIT)
版权所有 (c) 2016 alfonsmartinez
特此授予任何人免费获得此软件及其相关文档副本(以下简称“软件”)的权利,无需许可,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本的权利,并允许向软件提供副本的个人这样做,前提是受以下条件约束:
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“原样”提供,不提供任何形式的保证,无论是明示的还是暗示的,包括但不限于适销性、特定用途的适用性和非侵权性。在任何情况下,作者或版权所有者都不对任何索赔、损害或其他责任负责,无论是基于合同、侵权或其他原因,由此软件或其使用或其他方式产生、产生于或与此软件的使用或其他方式有关。