Cakephp Auth登录保持重定向到UserController->登录

时间:2014-04-10 05:40:25

标签: cakephp authentication

简单的问题

以下是我的AppController中与我的所有控制器继承的Auth相关代码。

class AppController extends Controller {

    public $components = array(
        'DebugKit.Toolbar',
        'Session',
        'Auth'=>array(
            //destination after logging in, or auto friendly fowarding depending on what user was trying to access
            'loginRedirect'=>array('controller'=>'Access', 'action'=>'login'),  
            'logoutRedirect'=>array('controller'=>'Access', 'action'=>'logout'), 
            'authError'=>'You cannot access that page', //Error message whenever someone access a page without auth
            'authorize'=>array('Controller') //Where in our application that authorization will occur

        )
    );

这是我的Access Controller,它应该控制登录和注销

class AccessController extends AppController {

    public $helpers = array('Html', 'Form', 'Session', 'Js' => array('Jquery'));


    public function index() {
        echo "index";
    }


    public function login() {

        $this->layout = 'login';


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

                $this->redirect($this->Auth->redirect());
            } else {
                $this->Session->setFlash('Your username/password combination was incorrect');
            }
        }

    }



    public function logout() {

        $this->redirect($this->Auth->logout());
    }

无论何时我尝试访问某个页面以便提示登录页面,浏览器都会给我一个错误:

The action login is not defined in controller UsersController

现在我正在使用UsersController用于其他目的(不用于登录和注销),这就是为什么我在AppController中为登录/注销重定向指定了AccessController。

为什么要尝试拉出UsersController?

1 个答案:

答案 0 :(得分:3)

我想我已经修好了。本网站支持我的主张 http://boulderinformationservices.wordpress.com/2013/04/25/cakephp-logoutredirect-is-not-the-same-as-loginaction/

我必须在我的Auth数组中添加一个loginAction才能进入登录界面。显然loginRedirect不是我想象的那样。

class AppController extends Controller {

public $components = array(
    'DebugKit.Toolbar',
    'Session',
    'Auth'=>array(
        //destination after logging in, or auto friendly fowarding depending on what user was trying to access
        'loginRedirect'=>array('controller'=>'access', 'action'=>'login'),
        'loginAction'=>array('controller'=>'access', 'action'=>'login'),
        'logoutRedirect'=>array('controller'=>'access', 'action'=>'logout'), 
        'authError'=>'You cannot access that page', //Error message whenever someone access a page without auth
        'authorize'=>array('Controller') //Where in our application that authorization will occur

    )
);
相关问题