cakephp中的loginRedirect无法正常工作

时间:2013-09-13 05:13:23

标签: cakephp url-redirection cakephp-2.4

在我们的CakePHP应用程序中,我们尝试使用Auth组件进行登录。

这是AppController:

class AppController extends Controller {
    public $components = array(
        'Session',
        'Auth' => array(
            'loginRedirect' => array('controller' => 'homes', 'action' => 'dashboard'),
            'logoutRedirect' => array('controller' => 'pages', 'action' => 'home')
        )
    );

    public function beforeFilter() {
        $this->Auth->allow('index','logout','display','home');
    }

这是UsersController:

class UsersController extends AppController {

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('add');

    }

    public function login() {
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                $this->redirect($this->Auth->redirect());
            } else {
                $this->Session->setFlash(__('Invalid username or password, try again'));
            }
        }
    }

    public function logout() {
        $this->redirect($this->Auth->logout());
    }


    public function index() {
        $this->User->recursive = 0;
        $this->set('users', $this->paginate());
    }


    public function add() {
        if ($this->request->is('post')) {
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('Checkin now'));
                $this->redirect(array('action' => 'login'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        }
    }
}

然后按照以下步骤操作:

  1. 在浏览器中输入http://localhost/cakephp/
  2. 的网址
  3. 从主页home.ctp导航至http://localhost/cakephp/users/login进行登录,使用Login按钮
  4. 输入用户名&密码并单击“登录”按钮
  5. 然后重定向到之前访问过的home.ctp页面而不是AppController中提到的页面。
  6. 第二次尝试:

    1. 直接从网址
    2. 访问http://localhost/cakephp/users/login/
    3. 然后输入登录凭据,然后重定向到AppController中提到的正确页面。
    4. 为什么Auth组件表现得像这样......

2 个答案:

答案 0 :(得分:0)

检查您的appcontroller,您正在重定向从家到仪表板的登录,它无法将您从用户重定向到登录

改变你的:

app控制器

        'loginRedirect' => array('controller' => 'users', 'action' => 'dashboard'),

和usercontroller

   public function login() {

    if ($this->request->is('post')) {

        /* login and redirect to url set in app controller */

        if ($this->Auth->login()) {

            return $this->redirect(array('controller' => 'users','action' => 'dashboard'));

        }

        $this->Session->setFlash(__('Invalid username or password, try again'));

    }

}

答案 1 :(得分:0)

根据documentation,您必须按如下方式更改重定向:

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirectUrl());
            // Prior to 2.3 use `return $this->redirect($this->Auth->redirect());`
        } else {
            $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
        }
    }
}

请注意更改return $this->redirect($this->Auth->redirectUrl());