roolith/router

简单的PHP路由器

1.0.8 2022-09-26 18:50 UTC

This package is auto-updated.

Last update: 2024-08-26 23:13:41 UTC


README

一个简单的路由器类

安装

composer require roolith/router

基本用法

use Roolith\Route\Router;

require_once __DIR__ . '/PATH_TO_AUTOLOAD/autoload.php';

$router = new Router();
$router->setBaseUrl('http://localhost/your_project_root/');

$router->get('/', function() {
    return 'Roolith router';
});

$router->run();

更多用法

$router->get('/test', function() {
    return 'Test route';
});

$router->post('/test', function() {
    return 'post content';
});

$router->put('/test', function() {
    return 'put content';
});

$router->patch('/test', function() {
    return 'patch content';
});

$router->delete('/test', function() {
    return 'delete content';
});

$router->options('/test', function() {
    return 'options content';
});

路由参数

$router->get('user/{id}', function($id) {
    return 'User id '.$id;
});

$router->get('/user/{userId}/edit/{another}', function($userId, $another) {
    return 'get content {userId}: '.$userId.' {another}: '.$another;
});

一次性多个路由

$router->get(['user', 'profile'], function() {
    return ['name' => 'John', 'age' => 45];
});

一次性多个方法

$router->match(['GET', 'POST'], '/user', function() {
    return 'GET POST content.';
});

控制器方法

$router->get('controller', 'Demo\Controller@index');

命名路由

$router->get('controller', 'Demo\Controller@index')->name('controller.index');

通配符路由

$router->any('any', function() {
    return 'any content. Server request method:'. $_SERVER['REQUEST_METHOD'];
});

CRUD路由

$router->crud('/crud', function () {
    return 'crud content.';
});

上面的示例等同于

$router->get('/crud', function() {})->name('crud.index');
$router->get('/crud/create', function() {})->name('crud.create');
$router->get('/crud/{item}', function() {})->name('crud.show');
$router->get('/crud/{item}/edit', function() {})->name('crud.edit');
$router->post('/crud', function() {})->name('crud.store');
$router->put('/crud/{item}', function() {})->name('crud.update');
$router->patch('/crud/{item}', function() {})->name('crud.update');
$router->delete('/crud/{item}', function() {})->name('crud.destroy');

如果有控制器

$router->crud('/crud', 'Controller');

上面的示例等同于

$router->get('/crud',               'Controller@index')->name('crud.index');
$router->get('/crud/create',        'Controller@create')->name('crud.create');
$router->get('/crud/{item}',        'Controller@show')->name('crud.show');
$router->get('/crud/{item}/edit',   'Controller@edit')->name('crud.edit');
$router->post('/crud',              'Controller@store')->name('crud.store');
$router->put('/crud/{item}',        'Controller@update')->name('crud.update');
$router->patch('/crud/{item}',      'Controller@update')->name('crud.update');
$router->delete('/crud/{item}',     'Controller@destroy')->name('crud.destroy');

重定向路由

$router->redirect('/redirect', '/redirected');
$router->redirect('/redirect', '/redirected', 302);

可选参数路由

$router->get('name/{name?}', function($name = 'Default name') {
    return "Your name is - $name";
});

中间件

$router->get('/admin/dashboard', function() {
    return 'Dashboard content';
})->middleware(\Demo\AuthMiddleware::class);

分组路由

$router->group(['middleware' => \Demo\AuthMiddleware::class, 'urlPrefix' => 'user/{userId}', 'namePrefix' => 'user.'], function () use ($router) {
    $router->get('profile', function ($userId){
        return "profile route: User id: $userId";
    })->name('profile');

    $router->get('action/{actionId}', function ($userId, $actionId){
        return "action route: User id: $userId and action id $actionId";
    })->name('action');
});

获取所有路由列表

$router->getRouteList();

通过名称获取路由URL

$router->getUrlByName('controller.index');

用于开发

./vendor/bin/phpunit --testdox tests --stderr

预期的单元测试结果

Request
 ✔ Should get current url
 ✔ Should able to set base url
 ✔ Should get requested method
 ✔ Should get requested url without base url
 ✔ Should remove non allowed character from url string
 ✔ Should able to set and get request param

Response
 ✔ Should have header content type set to false
 ✔ Should able to set status code
 ✔ Should invoke once output html method if content is html
 ✔ Should invoke once output json method if content is array
 ✔ Should set json header
 ✔ Should set html header
 ✔ Should set plain header
 ✔ Should output json
 ✔ Should output html
 ✔ Should have error response
Something went wrong
Router
 ✔ Should initialize router array
 ✔ Should have group settings array
 ✔ Should able to set group settings
 ✔ Should able to reset group settings
 ✔ Should able to add get route
 ✔ Should able to add post route
 ✔ Should able to add put route
 ✔ Should able to add patch route
 ✔ Should able to add delete route
 ✔ Should able to add options route
 ✔ Should able to add multiple method route at once
 ✔ Should able to add wildcard route
 ✔ Should able to add crud route
 ✔ Should have default route for crud
 ✔ Should have create route for crud
 ✔ Should have show route for crud
 ✔ Should have edit route for crud
 ✔ Should have post route for crud
 ✔ Should have update route for crud
 ✔ Should have delete route for crud
 ✔ Should automatic define method name for controller for crud
 ✔ Should able to add redirect route
 ✔ Should able to group routes
 ✔ Should match router by path
 ✔ Should match router by pattern
 ✔ Should match pattern with given url
 ✔ Should add name to route
 ✔ Should get url by name
 ✔ Should add middleware to route
 ✔ Should route run call execute route method once