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']);
提前致谢!