Shail
Shail

Reputation: 1575

Zend Framework 2 Routing Error

I am trying to create a custom user module in Zend 2, but I am having a problem in routing in "Segment" and "Literal".

I have googled a lot without finding a solution. My module is "User":

I am getting an error when I try to access the URL siteurl/user/login or siteurl/user.

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

I already have a user controller in "module\User\src\User\Controller\UserController".

I have defined the route in 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',
        ),
    ),
);

What am I missing here?

Upvotes: 0

Views: 592

Answers (1)

Crisp
Crisp

Reputation: 11447

Your routes have no default '__NAMESPACE__' key defined, so the router doesn't know where to look for a controller named 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',
                ),
            ),
            // ..
        ),
    ),
),

Upvotes: 3

Related Questions