lablnet / zestrouter
Zest Router.
This package is auto-updated.
Last update: 2024-09-23 17:42:42 UTC
README
ZestRouter 是一个轻量但强大的 PHP 路由类
<?php use Lablnet\ZestRouter; require_once "../vendor/autoload.php"; $router = new ZestRouter; //Namespaces uses for loading controllers olny //$router->setDefaultNamespace("App\Controllers\\"); $router->get('', function () { echo 'Example route using closure'; }); /* //OR $router->add('', function () { echo 'Example route using closure'; },'GET'); */ $router->get('test','Home@index'); /* //OR $router->get('test',['controller' => 'Home', 'action' => 'index']); //OR $router->add('test',['controller' => 'Home', 'action' => 'index'],'GET'); */ //Dispatch/Process the request automatically for mannually dispatch request take a look at Process Request section $router->dispatch($_SERVER['QUERY_STRING']);
特性
- 支持所有 HTTP 方法
- 灵活的正则表达式路由
- 自定义正则表达式
- 通过命名空间使用控制器路由
- 使用闭包的路由器
安装
在终端/命令行中运行以下命令 composer require lablnet/zestrouter
入门
要求
- PHP 7 或更高版本
- Composer
重写所有请求
Apache (.htaccess)
# Remove the question mark from the request but maintain the query string
RewriteEngine On
# Uncomment the following line if your public folder isn't the web server's root
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
Nginx (nginx.conf)
location / {
if (!-f $request_filename){
set $rule_0 1$rule_0;
}
if (!-d $request_filename){
set $rule_0 2$rule_0;
}
if ($rule_0 = "21"){
rewrite ^/(.*)$ /index.php?$1 last;
}
}
添加路由
到现在为止,你应该已经重写了所有请求
简单(默认方式)
添加路由有多种方式
使用 add 方法
$router->add('', function () { echo "Welcome"; },'GET');
add() 方法接受以下参数。
$route (string) 这是匹配的路线模式。这可以是普通字符串或自定义正则表达式。
$params (array|string|Closure) 这是控制器和控制器方法的参数或闭包(数组)=> ['controller' => 'Home', 'action' => 'index'] (string) => "Home@index" (closure) => function () { echo "Welcome"; }
$method (string) 这是一个由管道分隔的字符串,表示接受的 HTTP 请求方法。
示例:GET|POST|PATCH|PUT|DELETE
使用 rest 方法
$router->get('', function () { echo "Welcome"; });
支持 5 种请求方法:`get(),post(),put(),patch(),delete()`
这些方法接受以下参数。
$route (string) 这是匹配的路线模式。这可以是普通字符串或自定义正则表达式。
$params (array|string|Closure) 这是控制器和控制器方法的参数或闭包(数组)=> ['controller' => 'Home', 'action' => 'index'] (string) => "Home@index" (closure) => function () { echo "Welcome"; }
示例添加路由
// add homepage using callable $router->get( '/home', function() { require __DIR__ . '/views/home.php'; }); // add users details page using controller@action string $router->get( 'users/{id:[0-9]+}', 'UserController@showDetails' );
为了快速添加多个路由,可以使用 addRoutes 方法。此方法接受一个数组或任何可遍历的类型。
$router->addRoutes(array( array('users/{username:\w+}', 'users@view', 'get'), array('users/{id:\d+}', 'users@update', 'PATCH'), array('users/{id:\d+}', 'users@delete', 'DELETE') ));
匹配请求
要匹配当前请求,只需调用不带任何参数的 customDispatch() 方法。
$match = $router->customDispatch();
如果找到匹配项,则 customDispatch() 方法将返回一个关联数组
处理请求
ZestRouter 会为您处理请求,但您可以使用您喜欢的任何方法。为了帮助您入门,这里有一个使用闭包的简化示例。
//Add the routes $router->get('', function () { echo 'Example route using closure'; }); $router->get('user/{id:\d+}', function ($args) { echo 'Example route using closure with params id: ' .$args['id']; }); // match current request url $match = $router->customDispatch(); if ($match && is_callable( $match['callable'] )) { call_user_func( $match['callable'], $match ); } else { // no route was matched echo '404 Not Found'; }