CakePHP:不能简单登录 [英] CakePHP: Cant do Simple Login
问题描述
我开始做蛋糕哈哈,每次我提交我的表格进行验证,没有什么发生!
i'm starting to cook cake haha, everytime that i submit my form for to make the authentication, nothing happens!
这是我的AppController
This's my AppController
App::uses('AuthComponent', 'Controller/Component');
class AppController extends Controller {
public $components = array(
'DebugKit.Toolbar',
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array(
'username' => 'email',
'password' => 'senha'
),
)
),
'loginRedirect' => array('controller' => 'users', 'action' => 'home'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'logout'),
'authError' => 'Sorry, You Can not Access it!',
'loginError' => 'Login or Password incorrets!',
'autoRedirect' => false
));
}
我的模型用户
class User extends AppModel
{
public $name ="User";
public function beforeSave($options = array()){
if (isset($this->data[$this->alias]['senha'])){
$this->data[$this->alias]['senha'] = AuthComponent::password($this->data[$this->alias]['senha']);
}
return true;
}
}
所以我的UsersControllers
just it, without validations! So My UsersControllers
class UsersController extends AppController
{
public $name = "Users";
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('adicionar');
}
public function index(){
}
public function home(){
$this->render('/home');
}
public function login(){
if($this->request->is('post')){
debug($this->request->data);
die();
if($this->Auth->login()){
$this->redirect($this->Auth->redirect());
}else{
$this->Session->setFlash(__('Mail or Password incorrets, Try again!'));
}
}
}
public function adicionar(){
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
}
}
how you can see, the action login is rendering the view Login, Is there any problem??
我的视图登录以进行身份验证。
And My View Login for authentication..
<?php echo $this->Form->create('User',array('url' => array('controller' => 'users', 'action' => 'login')));
echo $this->Form->input('email');
echo $this->Form->input('senha');
echo $this->Form->submit('Login');
echo $this->Form->end(); ?>
我在哪里?我无法验证..
Where am i my wrong? I cann't authenticate..
更新....
我如何说,debuggin我的项目我可以看到!我的请求数据不为空
How I had said, debuggin my project i can see! My request data is no empty
但我有条件限制else
but i fell conditional else
调试请求数据
img src =http://i.stack.imgur.com/HrvN3.gifalt =enter image description here>
推荐答案
// AppController
//AppController
App::uses('AuthComponent', 'Controller/Component');
class AppController extends Controller {
/**
* Components
*
* @var array
*/
public $components = array(
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array(
'username' => 'email',
'password' => 'senha'
),
)
),
'loginRedirect' => array('controller' => 'users', 'action' => 'home'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'logout'),
'authError' => 'Por favor, Faça o login para acessar a Área Restrita',
'loginError' => 'Login ou Senha incorreto!'
'autoRedirect' => false
);
视图 - 您必须以创建形式设置模型名称用户NOT用户
view - You have to set Model name User NOT Users in create form
<?php echo $this->Form->create('User',array('url' => array('controller' => 'users', 'action' => 'login'));
echo $this->Form->input('email');
echo $this->Form->input('senha');
echo $this->Form->end(); ?>
// controller
//controller
public function login()
{
if($this->request->is('post'))
{
if($this->Auth->login())
{
$this->redirect($this->Auth->redirect());
}else
{
$this->Session->setFlash('Your username/password was incorrect');
}
}
}
//添加到您的用户模型
//add this to your User model
App::uses('AppModel', 'Model');
/**
* User Model
*/
class User extends AppModel {
public function beforeSave($options = array()) {
if (!empty($this->data['User']['password'])) {
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
return true;
}
}
这里是简单验证登录
这篇关于CakePHP:不能简单登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!