preDispatch zend框架2无法正常工作

时间:2012-12-10 06:23:02

标签: php zend-framework2

我在理解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都受到高度重视。感谢

1 个答案:

答案 0 :(得分:0)

尝试更改仪表板路线中的括号,如下所示:

'route' => '/Dashboard[/:action[/:id]]',