简体   繁体   中英

Zend Framework 2 routing issue

I have just started learning ZF2, so I apologize if the question is trivial. What I want to achieve is to redirect the user from projectname.loc:8888/user to projectname.loc:8888/user/login If I enter projectname.loc:8888/user/login manually the form is displayed without any issues. If I enter projectname.loc:8888/user then I get the following error message: Route with name "login" not found.

The route setting is modul.config.php are:

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

public function indexAction() {
    return $this->redirect()->toRoute('user/login', array('controller'=>'user', 'action'=>'login'));
}
public function loginAction() {
    $form = new Login();

    return ['form' => $form];
}

I have a feeling that I have the wrong child_routes configuration but I cannot figure out the proper solution... :(

Any help is much appreciated!

It should be like this.

public function indexAction() {
    // it's user/process rather than user/login
    return $this->redirect()->toRoute('user/process', array('controller'=>'user', 'action'=>'login'));
}

For your information, zf2 routing will look for the route key.

'user' => array( /** toRoute('user', ... ) **/
        'type'    => 'Literal',
        'options' => array(
                 // ...
        ),
        'may_terminate' => true,
        'child_routes' => array(
            'process' => array( /** toRoute('user/process', ... )  **/
                // ...
            ),
            'some-other-route' => array( /** toRoute('user/some-other-route', ... ) **/
                // ...
            ),
        ),
    ),

The route name of the $this->redirect()->toRoute('user/login') relates to the array keys in you route config (not the route parameter as I assume your are confusing it with)

So all you need to do is add in a new 'login' route.

    'user' => array(
        'type'    => 'Literal',
        'options' => array(
            'route'    => '/user',
            'defaults' => array(
                '__NAMESPACE__' => 'TAuth\Controller',
                'controller'    => 'User',
                'action'        => 'index',
            ),
        ),
        'may_terminate' => true,
        'child_routes' => array(

            // Placed before 'process' so it would match first
            'login' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/login',
                    'defaults' => array(
                        // controller & namespace inherited from parent
                        'action' => 'login',
                    ),
                ),
                'may_terminate' => true,
            ),

            'process' => array(
                // .....
            ),
        ),
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM