ds/router

该包已被废弃,不再维护。未建议替代包。

带缓存FastRoute控制器处理器的路由装饰器

v1.0.0 2017-11-27 14:12 UTC

This package is not auto-updated.

Last update: 2018-01-20 10:32:59 UTC


README

Code Climate Test Coverage

Build Status

Scrutinizer Code Quality Code Coverage Build Status

Rs\Router

PHP 路由器

基于适配器的路由组件。

包含以下适配器

  • Fast Route(带有String|Closure的缓存路由)
  • Symfony 路由

创建路由器

$router = new Rs\Router\Router(
    Ds\Router\Interfaces\AdaptorInterface,
    Ds\Router\Interfaces\RouteCollectionInterface
)

//Fast Router with Serialized Routes.
$router = new Router(
    new FastRouteAdaptor(
        new SuperClosure(
            new Serializer()
        ),
        (array) $options
    ),
    new RouteCollection()
);

//Use the Factory methods for easy access
$router = \Ds\Router\RouterFactory::createFastRouter($options);

路由适配器

适配器必须实现 'Ds\Router\Interfaces\AdaptorInterface'

提供的适配器

  • FastRoute - Ds\Adaptors\FastRouteAdaptor
  • Symfony - Ds\Adaptors\SymfonyAdaptor
    Ds\Router\Adaptor\FastRouteAdaptor(
        SerializerInterface $serializer,
        array $options = []
    );


    $fastRoute = new FastRouteAdaptor(
         new SuperClosure(
            new Serializer()
         ),
         [
             'cacheDisabled' => FALSE,
             'cacheFile' => __DIR__ . '/routes.cache',
             'cacheExpires' => 60 * 60 * 24 * 7,
             'errorHandlers' => [
                 'default' => [
                     'handler' => '\Site\Controllers\Error\ErrorController::error404',
                     'name' => ['error']
                 ]
             ]
         ]
    )

$router = $router->withAdaptor($fastRoute)

路由处理程序

 //PSR Based.
 function (
    RequestInterface $request, 
    ResponseInterface $response, 
    callable $next = null
  ) {
        if ($next){
            $response = $next($request, $response);
        }
        
        return $response;
  }

路由示例

    $router->addRoute(
        'get', 
        '/simple', 
        function ($request, $response){
          return $response;
        },
        ['routeName']
     );

使用类分发器

  //Load Dispatcher to convert handler intro response.
  $dispatcher = new \Ds\Router\Dispatcher\Dispatcher([
    'whitelist' => ['ControllerInterface'],
    'blacklist' => ['ControllerInterface2'],
    'autoWire' => true
  ]);
  
  
  //$ontainer some DI-Container.  
  //Dispatch the request with params to pass to controller.
  $dispatched = $dispatcher->dispatch(
    $serverRequest,
    $response->getHandler(),
    ['container' => $container]  
  );
  
  var_dump($dispatched->getBody()->getContents());
  

路由集合

路由集合必须实现 \Ds\Router\Interfaces\RouteCollectionInterface。

$routeCollection = new \Ds\Router\RouteCollection();

$routeCollection->group('/sub-dir', function() use ($routeCollection){

    $routeCollection->addRoute(
            (string)$httpMethod,
            (string)$path,
            (string)$handler,
            (array)$names
    );
});

$router = $router->withCollection($routeCollection)

加载器

集合加载器必须实现 \Ds\Router\Interfaces\LoaderInterface。


//Or routes can be added via seperate files via the FileLoader
//Variables can be made accessible via 'vars' option.
$fileLoader = new Ds\Router\Loaders\FileLoader($router, [
    'vars' => [
        'varName' => $varName,
    ]
]);

//load routes from Routes/routes.php and Routes/routes2.php
$router = $fileLoader->loadFiles([
    __DIR__ . '/path/to/file.php',
]);

#### 路由示例


使用路由器。

一旦添加了适配器和路由,就可以将路由与PSR7请求匹配,并返回RS\Router\RouterResponse。

分发路由。

使用 Router::getRouterResponse() 返回该请求的 RouterResponse。

分发处理程序。

处理程序必须从控制器/闭包中解析出来才能从控制器/闭包中获取响应。这在与昂贵的调用或可能在前/后中间件之前/之后无法访问的网站区域(例如认证)处理时非常有用。在不解析的情况下尝试 RouterResponse::getResponse() 将引发异常。