1

我在理解 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 上运行?

有人可以向正确的方向投光并引导。任何建议/意见/帮助都受到高度重视。谢谢

4

1 回答 1

0

尝试更改 Dashboard 路由中的括号,如下所示:

'route' => '/Dashboard[/:action[/:id]]',
于 2012-12-11T08:19:29.783 回答