jsl/router

一个路由器

0.2.6 2023-08-06 19:05 UTC

This package is auto-updated.

Last update: 2024-08-27 13:08:21 UTC


README

快速示例

// Load composers autoloader
require __DIR__ '/path/to/vendor/autoload.php';

// Import and instantiate the router
use Jsl\Router\Router;

$router = new Router;

$router->get('/some/path', function () {
    return 'Response for URL /some/path';
});

// Run the router and get the response from the callback
$response = $router->run();

echo $response; 
// If the URL is /some/path, then you'll see "Response for URL /some/path"

添加路由

添加路由的不同方法

// Add a GET route
$router->get('/foo', function () {
    return 'This is the callback for GET /foo';
});

// Add a POST route
$router->post('/foo', function () {
    return 'This is the callback for POST /foo';
});

//...same for put(), delete()

// Allow any verb
$router->any('/foo', function () {
    return 'This is the callback for any verb on /foo';
});

// Add a route for some other verb (PATCH is just an example)
$router->addRoute('PATCH', '/foo', function () {
    return 'This is the callback for PATCH /foo';
});

使用属性添加路由

// Add attributes to the class

// Add optional group info, like a prefix for all routes in the class
#[JslRouteGroup(prefix: '/foo')]
class SomeClass
{
    // Add a route for this method
    #[JslRoute(
        method: 'GET',
        path: '/bar',
        name: 'someName'
    )]
    public function someMethod()
    {
        return 'Callback for route /foo/bar';
    }
}


// Pass the class name to the router
$router->addRoutesFromClassAttributes(SomeClass::class);

// Or pass a list of class names
$router->addRoutesFromClassAttributes([
    SomeClass::class,
    SomeOtherClass::class,
]);

回调

路由回调有不同的形状和大小

// Closure
$route->get('/foo', function () { ... });

// Passing class name and method name - The router will instantiate the class when running the router
$router->get('/foo', ['Some\ClassName', 'someMethod']);

// Passing instance and method name
$router->get('/foo', [$somClass, 'someMethod']);

// ...you can use any callable as callback

路由参数

添加动态路由参数

// (:num) - Matches only numbers (uses regex: [\d]+)
$router->get('/foo/(:num)', function ($param) {
    return "This is a route param: {$param}";
});

// (:hex) - Matches only hexadecimal characters (uses regex: [a-fA-F0-9]+)
$router->get('/foo/(:hex)', function ($param) {
    return "This is a route param: {$param}";
});

// (:any) - Matches any characters (up to the next /) (uses regex: [^\/]+)
$router->get('/foo/(:any)', function ($param) {
    return "This is a route param: {$param}";
});

// (:all) - Matches everything (including /) (uses regex: .*)
$router->get('/foo/(:all)', function ($param) {
    return "This is a route param: {$param}";
});

// Make a dynamic paramter optional by adding ?
$router->get('/foo/(:num)?', function ($param = 123) {
    return "This is a route param: {$param} or 123 if no param was passed";
});

分组路由

// Add a prefix to all routes in the group
$router->group(['prefix' => '/foo'], function (Router $router) {
    // This will add a route for /foo/bar
    $router->get('/bar', function () { ... });

    // This will add a route for /foo/example
    $router->get('/example', function () { ... });
});

// Use dynamic parameters in the prefix, just like in the routes
$router->group(['prefix' => '/foo/(:num)'], function (Router $router) {
    // This will add a route for /foo/(:num)/bar
    $router->get('/bar', function () { ... });
})

命名路由

为了避免记住所有URL并手动更新/同步链接,您可以使用命名路由

// Add a route and give it a name
$router->get('/foo', function () { ... })->setName('something-foo');

// Get the route for a specific name
$path = $router->getNamedRoute('something-foo'); 
// Returns: /foo

// Add a route with dynamic parameters and give it a name
$router->get('/foo/(:num)', function () { ... })->setName('something-foo');

// Get the route for a specific name with dynamic parameters
// Pass the values for each dynamic route parameter as an array
$path = $router->getNamedRoute('something-foo', [123]); 
// Returns: /foo/123

解析回调

如果您传入一个类名: ['ClassName', 'methodName']'ClassName' 并且它有 __invoke() 方法,则需要实例化该类。当您调用 run() 时,路由器会为您执行实例化(例如,通过一些IoC容器注入依赖项),但如果您想控制实例化(例如,通过某些IoC容器注入依赖项),则可以设置自己的自定义类解析器。

// An example that does what the default resolver does
$router->setClassResolver(function (string $className): object {
    return new $className;
});

您函数的参数始终是字符串,并且响应必须是类实例。
上述只是一个最小(且相当无用)的示例。您的函数可以执行您想要的任何事情(比如通过容器解析),只要您返回一个类实例。