1

Zend Expressive 有 Aura.Router、FastRoute 和 zend-mvc 路由器的适配器,并且路由可以轻松匹配方法和路径:

<?php
$app->get('/foo', $middleware);

使用zend-mvc 路由器组件可以匹配主机名:

<?php
use Zend\Mvc\Router\Http\Hostname;

$route = Hostname::factory([
    'route' => ':subdomain.example.com/foo',
    'constraints' => [
        'subdomain' => 'api',
    ],
]);

$router->addRoute('foo', $route);

这也可以通过 Symfony路由组件实现:

<?php
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;

$route = new Route(
    '/foo', // path
    array('_controller' => 'SomeController'), // default values
    array('subdomain' => 'api'), // requirements
    array(), // options
    '{subdomain}.example.com', // host
    array(), // schemes
    array() // methods
);

$routes = new RouteCollection();
$routes->add('foo', $route);

因此,我希望能够使用 Expressive 做类似的事情,并根据子域将请求分派到不同的中间件:

// dispatch the requiest to ApiMiddleware
$app->get(':subdomain.example.com/foo', $ApiMiddleware, ['subdomain' => 'api']);

// dispatch the requiest to WebMiddleware
$app->get(':subdomain.example.com/foo', $WebMiddleware, ['subdomain' => 'www']);

提前致谢!

4

1 回答 1

2

为什么不创建一个新的中间件来提取请求并选择接下来应该在管道中调用哪个中间件?

你可以这样做:

应用程序\中间件\决策者中间件

<?php

namespace Application\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class DeciderMiddleware
{
    protected $apiMiddleware;
    protected $webMiddleware;

    public function __construct(
        callable $apiMiddleware,
        callable $webMiddleware
    ) {
        $this->apiMiddleware = $apiMiddleware;
        $this->webMiddleware = $webMiddleware;
    }

    public function __invoke(
        ServerRequestInterface $request,
        ResponseInterface $response,
        callable $next = null
    ) {
        if (strpos($request->getUri()->getHost(), 'api.') === 0) {
            return ($this->apiMiddleware)($request, $response);
        }

        if (strpos($request->getUri()->getHost(), 'www.') === 0) {
            return ($this->webMiddleware)($request, $response);
        }

        return $next($request, $response);
    }
}

config/autoload/middleware-pipeline.global.php

<?php

return [
    'dependencies' => [
        'factories' => [
            Application\Middleware\DeciderMiddleware::class => Application\Middleware\DeciderMiddlewareFactory::class
        ],
    ],
    'middleware_pipeline' => [
        'always' => [
            'middleware' => [
                Application\Middleware\DeciderMiddleware::class
            ],
            'priority' => 10000,
        ],
    ],
];

您唯一需要做的就是在 DeciderMiddlewareFactory 中定义 apiMiddleware 和 webMiddleware 并使用这些参数初始化 DeciderMiddleware-object。

于 2017-02-04T12:00:38.610 回答