idealogica/route-one

PSR-15 路由中间件,用于高级中间件路由。

0.1.4 2020-05-24 08:25 UTC

This package is auto-updated.

Last update: 2024-09-24 18:55:09 UTC


README


route-one

该软件包处于测试阶段

route-one 是一个兼容 PSR-15 的中间件,旨在根据 HTTP 请求 URL 路径、主机、HTTP 方法等灵活地将请求路由到另一个中间件。它基于 MiddlemanAura.Router 软件包构建。它是您喜欢的中间件调度器的良好补充,可以让它更像是传统的请求路由器。该软件包还包含一个中间件调度器,它包含许多有用的方法,便于创建路由中间件实例。

route-one 与现代框架中的经典控制器路由器非常相似,但它有一些额外的优势

  • 符合标准。您可以使用任何 PSR-15 兼容的中间件。例如,以下任何一个:middlewares/psr15-middlewares
  • 允许构建多维路由并修改来自一组中间件的反应。
  • 这使得您的代码高度可重用。Web 资源的任何部分都可以作为单独的软件包打包中间件,并在其他项目中使用。

安装

composer require idealogica/route-one:~0.1.0

通用用法

// general dispatcher

$dispatcher = DispatcherFactory::createDefault()->createDispatcher();

// page layout middleware

$dispatcher->addMiddleware(
    function (ServerRequestInterface $request, DelegateInterface $next) {
        $response = $next->process($request);
        $content = $response->getBody()->getContents();
        return $response
            ->withBody($this->streamFor('<html><body>' . $content . '</body></html>'))
            ->withHeader('content-type', 'text/html; charset=utf-8');
    }
);

// blog middleware

$dispatcher->addMiddleware(
    function (ServerRequestInterface $request, DelegateInterface $next) {

        // blog middleware dispatcher

        $blogDispatcher = DispatcherFactory::createDefault()->createDispatcher();
        $blogDispatcher->getDefaultRoute()->setHost('www.test.com')->setSecure(false);

        // blog posts list middleware (path based routing)

        $blogDispatcher->addGetRoute('/blog/posts',
            function (ServerRequestInterface $request, DelegateInterface $next) {
                // stop middleware chain execution
                return new Response($this->streamFor('<h1>Posts list</h1><p>Post1</p><p>Post2</p>'));
            }
        )->setName('blog.list');

        // blog single post middleware (path based routing)

        $blogDispatcher->addGetRoute('/blog/posts/{id}',
            function (ServerRequestInterface $request, DelegateInterface $next) {
                $id = (int)$request->getAttribute('1.id'); // prefix for route-one attributes
                // post id is valid
                if ($id === 1) {
                    // stop middleware chain execution
                    return new Response($this->streamFor(sprintf('<h1>Post #%s</h1><p>Example post</p>', $id)));
                }
                // post not found, continue to the next middleware
                return $next->process($request);
            }
        )->setName('blog.post');

        // blog page not found middleware (no routing, executes for each request)

        $blogDispatcher->addMiddleware(
            function (ServerRequestInterface $request, DelegateInterface $next)
            {
                // 404 response
                return new Response($this->streamFor('<h1>Page not found</h1>'), 404);
            }
        );

        return $blogDispatcher->dispatch($request);
    }
);

// dispatching

$response = $dispatcher->dispatch(
    new ServerRequest(
        [],
        [],
        'http://www.test.com/blog/posts/1',
        'GET'
    )
);

(new SapiEmitter())->emit($response);

如您所见,您可以使用可调用的作为中间件。它不是 PSR-15 的一部分,但它对于快速原型设计非常有帮助。

您还可以将路由中间件与您自己的调度器单独使用

$routeFactory = new RouteFactory(
    new AuraRouteMiddleware($basePath),
    new AuraUriGenerator($basePath)
);
$routeMiddleware = $routeFactory->createGetRoute('/blog/posts', $middlewareToRoute);

有关路由规则配置的更多信息,请参阅 Aura.Router 路由文档

URI 生成

您可以使用生成器根据命名路由创建 URI。例如上述代码的示例

// route path is '/blog/posts/{id}' for 'blog.post' route
$blogPostUrl = $blogDispatcher->getUriGenerator()->generate('blog.post', ['id' => 100]);
echo($blogPostUrl); // outputs "http://www.test.com/blog/posts/100"

有关 URI 生成的更多信息,请参阅 Aura.Router 生成器文档

许可

route-one 在 MIT 许可证 下授权。