PHP code example of guirong / php-router
1. Go to this page and download the library: Download guirong/php-router library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
guirong / php-router example snippets
composer
use Guirong\PhpRouter\Router;
$router = new Router();
use Guirong\PhpRouter\Router;
// 需要先加载 autoload 文件
'hello';
});
// 开始调度运行
$app = new \Guirong\PhpRouter\App\Application($router);
$response = $app->handle(
$request = $_REQUEST, //你的请求资源,可自定义
);
$response->send();
// 匹配 GET 请求. 处理器是个闭包 Closure
$router->get('/', function() {
echo 'hello';
});
// 匹配参数 'test/john'
$router->get('/test/{name}', function($params) {
echo $params['name']; // 'john'
}, [
'name' => '\w+', // 添加参数匹配限制。若不添加对应的限制,将会自动设置为匹配除了'/'外的任何字符
]);
// 可选参数支持。匹配 'hello' 'hello/john'
$router->get('/hello[/{name}]', function() {
echo $params['name'] ?? 'No input'; // 'john'
}, [
'name' => '\w+', // 添加参数匹配限制
]);
// 匹配 POST 请求
$router->post('/user/login', function() {
var_dump($_POST);
});
// 匹配 GET 或者 POST
$router->map(['get', 'post'], '/user/login', function() {
var_dump($_GET, $_POST);
});
// 允许任何请求方法
$router->any('/home', function() {
echo 'hello, you request page is /home';
});
$router->any('/404', function() {
echo "Sorry,This page not found.";
});
// 路由组
$router->group('/user', function ($router) {
$router->get('/', function () {
echo 'hello. you access: /user/';
});
$router->get('/index', function () {
echo 'hello. you access: /user/index';
});
});
// 使用 控制器
$router->get('/', App\Controllers\HomeController::class);
$router->get('/index', 'App\Controllers\HomeController@index');
$router->any('*', 'fallback_handler');
/hello[/{name}] // match: /hello/tom /hello
/my[/{name}[/{age}]] // match: /my/tom/78 /my/tom
/my[/{name}]/{age}
'autoRoute' => 1, // 启用
'controllerNamespace' => 'App\\Controllers', // 控制器类所在命名空间
// set config
$router->config([
'ignoreLastSlash' => true,
'autoRoute' => 1,
'controllerNamespace' => 'app\\controllers',
]);
array public function match($path, $method)
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$method = $_SERVER['REQUEST_METHOD'];
$routeInfo = $router->match($path, $method);
use Guirong\PhpRouter\Dispatcher\Dispatcher;
$dispatcher = new Dispatcher([
// default action method name
'defaultAction' => 'index',
'actionPrefix' => '',
'dynamicAction' => true,
// @see Router::$globalParams['act']
'dynamicActionVar' => 'act',
]);
// 当匹配失败, 重定向到 '/404'
$dispatcher->on('notFound', '/404');
// 或者, 当匹配失败, 输出消息...
$dispatcher->on('notFound', function () {
echo "the page not found!";
});
// 当请求方式不允许, 仅允许 POST/GET 等, 输出消息...
$dispatcher->on('methodNotAllowed', function ($uri) {
echo "the method not allowed!";
});
$app = new \Guirong\PhpRouter\App\Application($router);
$response = $app->setDispatcOptions(
['dispatcher' => $dispatcher]
)->handle(
$request = $_REQUEST, //你的请求资源,可自定义
);
$response->send();
$router->get('/', App\Controllers\HomeController::class);
$router->get('/index', 'App\Controllers\HomeController@index');
$router->get('/about', 'App\Controllers\HomeController@about');
'dynamicAction' => true, // 启用
// action 方法名匹配参数名称,符合条件的才会当做action名称
// @see Router::$globalParams['act'] 匹配 '[a-zA-Z][\w-]+'
'dynamicActionVar' => 'act',
// 访问 '/home/test' 将会执行 'App\Controllers\HomeController::test()'
$router->any('/home/{act}', App\Controllers\HomeController::class);
// 可匹配 '/home', '/home/test' 等
$router->any('/home[/{act}]', App\Controllers\HomeController::class);
// 访问 '/user', 将会调用 App\Controllers\UserController::run('')
$router->get('/user', 'App\Controllers\UserController');
// 访问 '/user/profile', 将会调用 App\Controllers\UserController::run('profile')
$router->get('/user/profile', 'App\Controllers\UserController');
// 同时配置 'actionExecutor' => 'run' 和 'dynamicAction' => true,
// 访问 '/user', 将会调用 App\Controllers\UserController::run('')
// 访问 '/user/profile', 将会调用 App\Controllers\UserController::run('profile')
$router->any('/user[/{name}]', 'App\Controllers\UserController');
$app = new \Guirong\PhpRouter\App\Application($router);
$response = $app->handle(
$request = $_REQUEST, //你的请求资源,可自定义
);
$response->send();
namespace App\Middleware;
use Closure;
class BeforeMiddleware
{
public function handle($request, Closure $next)
{
// 你可以在这里执行一些操作 ...
return $next($request);
}
public function terminate($request,$response){
// 请求响应完成后执行操作
}
}
namespace App\Middleware;
use Closure;
class AfterMiddleware
{
public function handle($request, Closure $next)
{
$response = $next($request);
// 执行操作
return $response;
}
public function terminate($request,$response){
// 请求响应完成后执行操作
}
}
namespace App\Middleware;
use Guirong\PhpRouter\Middleware\Config;
class MyConfig extends Config
{
/**
* The application's middleware stack.
* Global middleware
* @var array
*/
protected $middleware = [
// \App\Middleware\BeforeMiddleware::class
];
/**
* The application's route middleware.
*
* @return array
*/
protected $routeMiddleware = [
'before' => \App\Middleware\BeforeMiddleware::class,
'after' => \App\Middleware\AfterMiddleware::class,
];
}
$router->middleware(['before','after'])->get('/user', 'App\Controllers\UserController');
$router->middleware('before','after')->get('/user', 'App\Controllers\UserController');
$router->middleware('before','after')->group('/user', function ($router) {
$router->get('/', function () {
echo 'hello. you access: /user/';
});
$router->get('/index', function () {
echo 'hello. you access: /user/index';
});
});
$router->group('/user', function ($router) {
$router->get('/', function () {
echo 'hello. you access: /user/';
});
$router->get('/index', function () {
echo 'hello. you access: /user/index';
});
},['before','after']);
$app = new \Guirong\PhpRouter\App\Application($router);
$response = $app->handle(
$request = $_REQUEST, //你的请求资源,可自定义
\App\Middleware\MyConfig::class //你的中间件配置类,可自定义
);
$response->send();
$app->terminate($request, $response);
$response = $app->handle(
$request = $_REQUEST, //你的请求资源,可自定义
\App\Middleware\MyConfig::class //你的中间件配置类,可自定义
);
if($response->getException()){
// 提取异常信息
$exception = $response->getException();
// ......自行处理异常
}
json
{
"uirong/php-router": "dev-master"
}
}
bash
php example/benchmark.php