zendframework 2中的路由无法正常工作

时间:2013-12-11 10:30:51

标签: php zend-framework zend-framework2

/* Here is my module config */



'controllers' => array(
    'invokables' => array(
    'User\Controller\User' => 'User\Controller\UserController',
    ),
),

'router' => array(
    'routes' => array(

          'user' => array(
            'type' => 'Literal',
            'options' => array(
                'route'    => '/user',
                'defaults' => array(
                    'controller' => 'User\Controller\User',
                    'action'     => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'default' => array(
                    'type'    => '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(

                        ),
                    ),
                ),
            ),
        ),
    ),
),

和控制器

类UserController扩展了AbstractActionController {

public function indexAction(){
    parent::indexAction();
    return new ViewModel();
}

public function addAction(){
    return new ViewModel();
}

}

每当我尝试访问zf.localhost / user / user / add

它会抛出错误

找不到页面。

请求的控制器无法映射到现有的控制器类。

控制器:     user(解析为无效的控制器类或别名:user)

没有例外

我无法弄清楚为什么路由无效。

1 个答案:

答案 0 :(得分:1)

您正在尝试访问不存在的控制器命名用户。您有两种选择:

  1. 'route' => '/[:controller][/:action]'更改为'route' => '/:action'。它将在User \ Controller \ UserAction
  2. 中搜索一个动作
  3. 添加到controllers新别名'aliases' => array('user' => 'User\Controller\User')