设想:
有一个名为 fabric 的 zf2 应用程序,它包括两个不同的模块,名为“bike”和“car”,并通过以下 url 结构提供类似(但不相同)的功能:
+------------------+-------+-------------------------+-------------+-----------------+--------+
| Hostname | Part | Page | Module | Controller | Action |
+------------------+-------+-------------------------+-------------+-----------------+--------+
| www.fabric.dev | / | Company homepage | Application | IndexController | index |
| bikes.fabric.dev | / | Homepage for bikes | Bike | IndexController | index |
| bikes.fabric.dev | /list | Listing of bikes | Bike | ListController | list |
| cars.fabric.dev | / | Homepage for cars | Car | IndexController | index |
| cars.fabric.dev | /list | Listing of bikes | Car | ListController | list |
+------------------+-------+-------------------------+-------------+-----------------+--------+
所以,
- Application、Bike 和 Car 是不同的模块和命名空间
\Application
,\Bike
\Car
- http 服务器上有 3 个虚拟主机(www..,cars..,bikes..)指向同一目录:
/www/fabric.dev/public
每个模块在 module.config.php 文件中都有自己的路由定义,如下面的非工作示例:
// module/Application/config/module.config.php 'router' => array( 'routes' => array( 'home' => array( 'type' => 'Hostname', 'options' => array( 'route' => 'www.fabric.dev', 'defaults' => array( '__NAMESPACE__' => 'Application\Controller' 'controller' => 'Application\Controller\Index', 'action' => 'index', ), ) ) ) );
和
// module/Bike/config/module.config.php
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Hostname',
'options' => array(
'route' => 'bikes.fabric.dev',
'defaults' => array(
'__NAMESPACE__' => 'Bike\Controller'
'controller' => 'Bike\Controller\Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Literal',
'options' => array(
'route' => '/list',
'defaults' => array(
'controller' => 'Bike\Controller\List',
'action' => 'list',
),
),
),
),
),
)
)
问题和疑问
网络上有足够多的文档分别介绍 zf2 的路由类,如主机名、段、文字和正则表达式,但没有关于如何配对和一起使用这些奇妙类的文档。
除了将所有应用程序源代码复制到单独的 vhost 目录之外,是否有任何适当的方法可以使用路由器配置提供上述 uri 方案?