登录时重定向

时间:2012-03-22 19:31:27

标签: cakephp cakephp-2.0

我希望用户在登录时重定向到帖子索引操作。但他们反而被重定向到帖子的添加动作。 下面是我的routes.php

    Router::connect('/', array('controller' => 'posts', 'action' => 'index'));
/**
 * ...and connect the rest of 'Pages' controller's urls.
 */
    Router::connect('/pages/*', array('controller' => 'posts', 'action' => 'index'));

我在用户控制器中的登录操作是

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash('Your username or password was incorrect.');
        }
    }
}

**调试**

当我在登录时进行调试时,我得到了这个

/app/Controller/UsersController.php (line 20)
object(AuthComponent) {
    components => array(
        (int) 0 => 'Session',
        (int) 1 => 'RequestHandler'
    )
    authenticate => array(
        (int) 0 => 'Form'
    )
    authorize => array(
        'Actions' => array(
            'actionPath' => 'controllers'
        )
    )
    ajaxLogin => null
    flash => array(
        'element' => 'default',
        'key' => 'auth',
        'params' => array()
    )
    loginAction => array(
        'controller' => 'users',
        'action' => 'login'
    )
    loginRedirect => array(
        'controller' => 'posts',
        'action' => 'add'
    )
    logoutRedirect => array(
        'controller' => 'users',
        'action' => 'login'
    )

由于某种原因它说

loginRedirect =>阵列(         'controller'=> “帖子”,         'action'=> '加'     )

代替action => index它是unsing action =>加。为什么

1 个答案:

答案 0 :(得分:1)

  

Auth-> redirect()返回用户在进入登录页面或Auth-> loginRedirect之前登陆的网址。

https://stackoverflow.com/a/2636940/643500

因此,如果您设置索引的根路径,它可能会起作用。