Zend2 路由 __NAMESPACE__ 不起作用或被忽略
Zend2 Routing __NAMESPACE__ doesn't work or be ignored
我在 Zend2 中使用这个配置作为我的应用程序模块配置,这真的很正常,每个建议都作为标准路由规则:
'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(resolves to invalid controller class or alias: Index)
对于 http://localhost/application/index/index
我得到:
index(resolves to invalid controller class or alias: 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',
),
),
如您所知,配置将合并,我应该在项目中只有一个索引控制器。
为什么 '__NAMESPACE__' => 'Application\Controller',
行被忽略,它只查找控制器数组中不存在的索引或索引??
编辑:
与其他项目相比,我将其添加到 Application/Module.php
:
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
}
现在可以使用了,但我需要解释。这是解决方案吗?我的意思是我应该将它添加到项目中的 Module.php
文件之一以使路由规则正常工作吗?为什么没有它 __NAMESPACE__
将在路由规则中被忽略?
您已经找到了解决方案,添加 ModuleRouteListener
是正确的做法。解释可以在the description of the onRoute
method inside this listener:
中找到
Listen to the "route" event and determine if the module namespace should be prepended to the controller name.
If the route match contains a parameter key matching the MODULE_NAMESPACE
constant, that value will be prepended, with a namespace separator, to the matched controller parameter.
我在 Zend2 中使用这个配置作为我的应用程序模块配置,这真的很正常,每个建议都作为标准路由规则:
'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(resolves to invalid controller class or alias: Index)
对于 http://localhost/application/index/index
我得到:
index(resolves to invalid controller class or alias: 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',
),
),
如您所知,配置将合并,我应该在项目中只有一个索引控制器。
为什么 '__NAMESPACE__' => 'Application\Controller',
行被忽略,它只查找控制器数组中不存在的索引或索引??
编辑:
与其他项目相比,我将其添加到 Application/Module.php
:
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
}
现在可以使用了,但我需要解释。这是解决方案吗?我的意思是我应该将它添加到项目中的 Module.php
文件之一以使路由规则正常工作吗?为什么没有它 __NAMESPACE__
将在路由规则中被忽略?
您已经找到了解决方案,添加 ModuleRouteListener
是正确的做法。解释可以在the description of the onRoute
method inside this listener:
Listen to the "route" event and determine if the module namespace should be prepended to the controller name.
If the route match contains a parameter key matching the
MODULE_NAMESPACE
constant, that value will be prepended, with a namespace separator, to the matched controller parameter.