【问题标题】:Zend Framework 2 Routing ErrorZend Framework 2 路由错误
【发布时间】:2013-05-19 07:44:53
【问题描述】:

我正在尝试在 Zend 2 中创建自定义用户模块,但在“Segment”和“Literal”中的路由出现问题。

我用谷歌搜索了很多,但没有找到解决方案。我的模块是“用户”:

当我尝试访问 URL siteurl/user/login or siteurl/user 时出现错误。

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

我已经在“module\User\src\User\Controller\UserController”中有一个用户控制器。

我在module.config.php 中定义了路由。

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

    // The following section is new and should be added to your file
    'router' => array(
        'routes' => array(
            'user' => array(
                'type' => 'Literal',
                'priority' => 1000,
                'options' => array(
                    'route' => '/user',
                    'defaults' => array(
                        'controller' => 'user',
                        'action'     => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'login' => array(
                        'type' => 'Literal',
                        'options' => array(
                            'route' => '/user/login',
                            'defaults' => array(
                                'controller' => 'user',
                                'action'     => 'login',
                            ),
                        ),
                    ),
                    'authenticate' => array(
                        'type' => 'Literal',
                        'options' => array(
                            'route' => '/user/authenticate',
                            'defaults' => array(
                                'controller' => 'user',
                                'action'     => 'authenticate',
                            ),
                        ),
                    ),
                    'logout' => array(
                        'type' => 'Literal',
                        'options' => array(
                            'route' => '/user/logout',
                            'defaults' => array(
                                'controller' => 'user',
                                'action'     => 'logout',
                            ),
                        ),
                    ),
                    'register' => array(
                        'type' => 'Literal',
                        'options' => array(
                            'route' => '/user/register',
                            'defaults' => array(
                                'controller' => 'user',
                                'action'     => 'register',
                            ),
                        ),
                    ),
                    'changepassword' => array(
                        'type' => 'Literal',
                        'options' => array(
                            'route' => '/user/change-password',
                            'defaults' => array(
                                'controller' => 'user',
                                'action'     => 'changepassword',
                            ),
                        ),                        
                    ),
                    'changeemail' => array(
                        'type' => 'Literal',
                        'options' => array(
                            'route' => '/user/change-email',
                            'defaults' => array(
                                'controller' => 'user',
                                'action' => 'changeemail',
                            ),
                        ),                        
                    ),
                ),
            ),
        ),
    ),

    'view_manager' => array(
        'template_path_stack' => array(
            'album' => __DIR__ . '/../view',
        ),
    ),
);

我在这里错过了什么?

【问题讨论】:

    标签: php zend-framework2


    【解决方案1】:

    您的路由没有定义默认的 '__NAMESPACE__' 键,因此路由器不知道在哪里寻找名为 user 的控制器

    // The following section is new and should be added to your file
    'router' => array(
        'routes' => array(
            'user' => array(
                'type' => 'Literal',
                'priority' => 1000,
                'options' => array(
                    'route' => '/user',
                    'defaults' => array(
                        // add the namespace
                        '__NAMESPACE__' => 'User\Controller'
                        'controller' => 'user',
                        'action'     => 'index',
                    ),
                ),
                // ..
            ),
        ),
    ),
    

    【讨论】:

    • 另一种方法是使用命名控制器。 IE。命名invokableuser
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    • 2014-05-02
    • 2015-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多