Php Zend2路由uu命名空间uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;不起作用或被忽视
我在Zend2中使用此配置作为我的应用程序模块配置,这非常正常,每个配置都建议作为标准路由规则:Php Zend2路由uu命名空间uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;不起作用或被忽视,php,zend-framework2,url-routing,Php,Zend Framework2,Url Routing,我在Zend2中使用此配置作为我的应用程序模块配置,这非常正常,每个配置都建议作为标准路由规则: 'controllers' => array( 'invokables' => array( 'Application\Controller\Index' => 'Application\Controller\IndexController', ), ), 'router' => array( 'routes' =&g
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController',
),
),
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
),
'application' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/application',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Zend\Mvc\Router\Http\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(
),
),
),
),
),
),
),
home
的路由工作正常。但是对于http://localhost/application
我得到:
索引(解析为无效的控制器类或别名:Index)
对于http://localhost/application/index/index
我得到:
索引(解析为无效的控制器类或别名:index)
如果我改变这一点:
'application' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/application',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
为此:
'application' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/application',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
如您所知,http://localhost/application
它会像home
url一样正常工作
如果我使用这个:
'controllers' => array(
'invokables' => array(
'index' => 'Application\Controller\IndexController',
),
),
正如您所知,配置将合并,我应该在项目中只有一个索引控制器
为什么要忽略行“\uuuu NAMESPACE\uuuu'=>“应用程序\控制器”
,并且它只在控制器数组中查找不存在的索引或索引
编辑:
与其他项目相比,我将其添加到了Application/Module.php
:
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
}
它现在起作用了,但我需要解释。这是解决办法吗?我的意思是,我应该将此添加到项目中的
Module.php
文件中,以使路由规则正常工作吗?为什么没有它,\uuu名称空间将在路由规则中被忽略?您已经找到了解决方案,添加模块OutElistener
是正确的做法。有关解释,请参见:
侦听“route”事件,并确定是否应在控制器名称前面加上模块名称空间
如果路由匹配包含与MODULE_NAMESPACE
常量匹配的参数键,则该值将在匹配的控制器参数前面加上名称空间分隔符
Thx,因此如果不使用ModuleRouteListener?@S.Gholizadeh,将不会检查名称空间