Zend Expressive

时间:2017-02-01 16:46:08

标签: php zend-framework symfony-routing zend-router zend-expressive

Zend Expressive有一个适用于Aura.Router,FastRoute和zend-mvc路由器的适配器,路由可以轻松匹配方法和路径:

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

使用zend-mvc Router组件可以匹配主机名:

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

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

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

Symfony Routing Component也可以这样做:

<?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']);

提前致谢!

1 个答案:

答案 0 :(得分:2)

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

你可以这样做:

应用程序\中间件\ DeciderMiddleware

<?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);
    }
}

配置/自动加载/中间件-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对象。