Just starting to learn the CakePHP framework. I’m using the Auth component, and all my actions that require a user to be logged in redirect to users/login rather than login/login (my non-standard controller). Anyone know where I can change this setting? Also, how does the auth component work across multiple controllers? Will I have to redefine this auto-redirect in every controller?
<?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'));
}
}
?>
If your entire website needs to be protected, then you can define the
Authcomponent in yourAppControllerwhich will cause those rules to apply to every controller that inherits from that object (i.e. all controllers on your site).The CakePHP Authentication Documentation outlines all the parameters you need in order to accomplish what you are trying to do. You should be able to define the login redirect when you setup the Auth component: