覆盖登录重定向

时间:2012-01-02 21:33:45

标签: php cakephp cakephp-2.0

刚开始学习CakePHP框架。我正在使用Auth组件,我需要用户登录的所有操作都会重定向到用户/登录,而不是登录/登录(我的非标准控制器)。有谁知道我可以在哪里更改此设置?此外,auth组件如何跨多个控制器工作?我是否必须在每个控制器中重新定义此自动重定向?

  <?php
  class LoginController extends AppController {

    public $name = 'Login';

    public $components = array('Auth');

    public $helpers = array('Html', 'Form' );

    function beforeFilter() {
            // tell Auth not to check authentication when doing the 'register' action
            $this->Auth->allow('register');
            $this->Auth->userModel = 'Login';
    }

    function register() {
            if (!empty($this->data)){
                    if ($this->Login->save($this->params['data'])) {
                            $this->flash('Your registration information was accepted. Welcome!');
                            $this->Auth->login($this->data);
                            $this->redirect(array('action' => 'index'));
                    } else {
                            $this->flash('There was a problem with your registration', '/login/knownusers');
                     }
             }      

    }

    function createprofile() {



    }

    function knownusers() {
            $this->set('knownusers', $this->Login->find(
                    'all',
                    array(
                             'fields' => array('id','username', 'password', 'fk_profile_id'),


                            $this->redirect(array('action' => 'index'));
                    } else {
                            $this->flash('There was a problem with your registration', '/login/knownusers');
                     }
             }      

    }

    function createprofile() {



    }

    function knownusers() {
            $this->set('knownusers', $this->Login->find(
                    'all',
                    array(
                             'fields' => array('id','username', 'password', 'fk_profile_id'),
                            'order' => 'id DESC'  
                    )
            ));
    }       

    function login() {
    }

    function logout() {
            $this->redirect($this->Auth->logout('login/login'));
    }

 }
 ?>

2 个答案:

答案 0 :(得分:2)

如果您的整个网站需要受到保护,那么您可以在Auth中定义AppController组件,这将导致这些规则应用于从该对象继承的每个控制器(即所有控制器都在你的网站。)

CakePHP Authentication Documentation概述了完成您要完成的任务所需的所有参数。您应该能够在设置Auth组件时定义登录重定向:

public $components = array(
    'Auth' => array(
        'loginAction' => array('controller' => 'User', 'action' => 'login')
    )
);

答案 1 :(得分:1)

您可以将此声明放在beforeFilter()

$this->Auth->loginAction = array('admin' => false, 'controller' => 'login', 'action' => 'login');

此外,您无需在每个控制器中定义此项。如果你需要为每个控制器执行任何逻辑,我会把它放在AppController的beforeFilter()中......

相关问题