问题
I'm starting with cakephp, and i'm trying to build a login system. The problem is that the server responds to my login requests reloading the login page, without any messages, and the login is not done, because it doesn't let me enter in protected pages.
This is my users table:
mysql> show columns from users;
+----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| email | varchar(255) | YES | | NULL | |
| password | varchar(255) | YES | | NULL | |
| role | varchar(20) | YES | | user | |
| created | datetime | YES | | NULL | |
| updated | datetime | YES | | NULL | |
| username | varchar(255) | YES | | NULL | |
+----------+------------------+------+-----+---------+----------------+
This is my user model:
<?php
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel{
var $name = "User";
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
return true;
}
}
?>
This is my appcontroller:
class AppController extends Controller {
public $components = array(
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home')
),
'Session'
);
public function beforeFilter() {
$this->Auth->allow('index', 'view');
$this->Auth->authorize = 'actions';
$this->Auth->loginError = "This message shows up when the wrong credentials are used";
$this->Auth->authError = "This error shows up with the user tries to access a part of the website that is protected.";
}
}
this is my userscontroller
<?php
class UsersController extends AppController{
public function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow('add', 'login');
}
public function index(){
...
}
public function view($id = null){
...
}
public function add(){
if($this->request->is("post")){
$this->User->create();
if($this->User->save($this->request->data)){
$this->Session->setFlash(__("The user has been saved"));
return $this->redirect(array("action" => "index"));
}
$this->Session->setFlash(__("The user could not be saved. Please try again"));
}
}
public function edit($id = null){
...
}
public function delete($id){
...
}
public function login(){
}
public function logout(){
$this->redirect($this->Auth->logout());
}
}
?>
来源:https://stackoverflow.com/questions/19385756/cakephp-login-does-nothing