我在理解preDispatch时遇到问题以下是我的代码
public function preDispatch(\Zend\Mvc\MvcEvent $e)
{
$application = $e->getApplication();
$serviceManager = $application->getServiceManager();
$controller = $e->getTarget();
$route = $controller->getEvent()->getRouteMatch();
$hit_controller = $route->getParam('__CONTROLLER__');
if(strcmp($hit_controller,"Dashboard")==0){
$authService = $serviceManager->get('Admin\Authentication\Service');
if (!$authService->hasIdentity()) {
$pluginManager = $serviceManager->get('Zend\Mvc\Controller\PluginManager');
$redirectPlugin = $pluginManager->get('redirect');
return $redirectPlugin->toRoute('Admin',array('controller'=>'Admin','action'=>'index'));
}
}
return;
}
以下是我在module.config.php中定义的路由
return array(
'router' => array(
'routes' => array(
'Dashboard' => array(
'type' => 'Segment',
'options' => array(
'route' => '/Dashboard[/:action][/:id]',
'defaults' => array(
'__NAMESPACE__' => 'Admin\Controller',
'controller' => 'Dashboard',
'action' => 'index',
),
),
),
'Admin' => array(
'type' => 'Segment',
'options' => array(
'route' => '/Admin[/:action][/:id]',
'defaults' => array(
'__NAMESPACE__' => 'Admin\Controller',
'controller' => 'Admin',
'action' => 'index',
),
),
),
),
),
'controllers' => array(
'invokables' => array(
'Admin\Controller\Admin' => 'Admin\Controller\AdminController',
'Admin\Controller\Dashboard' => 'Admin\Controller\DashboardController'
),
),
'view_manager' => array( ),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
);
如果我输入网址
public/Admin works fine
public/Dashboard works fine
public/Dashboard/edit works fine
public/Dashboard/edit/11 fails
并显示edit / 11的内容。我不明白为什么preDispatch不能在Dashboard / edit / 11上运行?
有人可以投光并指导正确的方向。任何建议/评论/ hlep都受到高度重视。感谢
答案 0 :(得分:0)
尝试更改仪表板路线中的括号,如下所示:
'route' => '/Dashboard[/:action[/:id]]',