sev*_*ien 8 php zend-framework2
经过长时间搜索没有成功.在我放弃之前,我想问一下:
有没有办法将子域路由到Zend Framework 2中的模块?喜欢:
子域 => 模块
api.site.com => api
dev.site.com => dev
admin.site.com => admin
site.com => public
...
我尝试这样做但我无法访问默认(索引)以外的控制器.
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Hostname',
'options' => array(
'route' => 'site.com',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
)
)
),
),
Run Code Online (Sandbox Code Playgroud)
感谢您抽出宝贵时间帮助我.
Zend Framework 2没有路由到模块的概念; 所有路由映射都在URI模式(用于HTTP路由)和特定控制器类之间.也就是说,Zend\Mvc提供了一个事件监听器(Zend\Mvc\ModuleRouteListener),它允许您定义基于给定模式映射到多个控制器的URI模式,从而模拟"模块路由".要定义这样的路由,您可以将其作为路由配置:
'router' => array(
'routes' => array(
// This defines the hostname route which forms the base
// of each "child" route
'home' => array(
'type' => 'Hostname',
'options' => array(
'route' => 'site.com',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
// This Segment route captures the requested controller
// and action from the URI and, through ModuleRouteListener,
// selects the correct controller class to use
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Index',
'action' => 'index',
),
),
),
),
),
),
),
Run Code Online (Sandbox Code Playgroud)
(点击此处查看@ ZendSkeletonApplication的示例)
不过,这只是方程式的一半.您还必须使用特定的命名格式注册模块中的每个控制器类.这也是通过相同的配置文件完成的:
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController'
),
),
Run Code Online (Sandbox Code Playgroud)
数组键是别名ModuleRouteListener将用于查找正确的控制器,它必须采用以下格式:
<Namespace>\<Controller>\<Action>
Run Code Online (Sandbox Code Playgroud)
分配给此数组键的值是控制器类的完全限定名称.
(点击此处查看@ ZendSkeletonApplication的示例)
注意:如果您没有使用ZendSkeletonApplication,或者已经删除了它的默认应用程序模块,则需要在自己的模块中注册ModuleRouteListener. 单击此处查看ZendSkeletonApplication如何注册此侦听器的示例