Zendframework 2 中的路由不起作用 [英] routing in zendframework 2 is not working

查看:27
本文介绍了Zendframework 2 中的路由不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/* 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{

class UserController extends AbstractActionController{

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

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

}

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

whenever I try to access zf.localhost/user/user/add

它抛出错误

未找到页面.

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

The requested controller could not be mapped to an existing controller class.

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

Controller: user(resolves to invalid controller class or alias: user)

没有可用的例外

我不明白为什么路由不起作用.

推荐答案

您正在尝试访问不存在的名为 user 的控制器.您有两个选择:

You are trying to access controller named user which doesn't exists. You have two options:

  1. 更改 'route' =>'/[:controller][/:action]''route' =>'/:action'.它将在您的 User\Controller\UserAction
  2. 中搜索操作
  3. 添加到 controllers 新别名 'aliases' =>array('user' => 'User\Controller\User')
  1. Change 'route' => '/[:controller][/:action]' to 'route' => '/:action'. And it will search for an action in your User\Controller\UserAction
  2. Add to controllers new alias 'aliases' => array('user' => 'User\Controller\User')

这篇关于Zendframework 2 中的路由不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆