問題描述
我開始做蛋糕了哈哈,每次我提交表單進行身份驗證時,沒有任何反應!
這是我的 AppController
App::uses('AuthComponent', 'Controller/Component');類 AppController 擴展控制器 {公共 $components = 數組('DebugKit.Toolbar','會議','認證' =>大批('認證' =>大批('表格' =>大批('字段' =>大批('用戶名' =>'電子郵件','密碼' =>'森哈'),)),'登錄重定向' =>數組('控制器' => '用戶','動作' => '家'),'注銷重定向' =>數組('控制器' => '用戶','動作' => '注銷'),'authError' =>'對不起,您無法訪問它!','登錄錯誤' =>'登錄名或密碼錯誤!','自動重定向' =>錯誤的));}
我的模型用戶
class User 擴展 AppModel{公共 $name ="用戶";公共函數 beforeSave($options = array()){if (isset($this->data[$this->alias]['senha'])){$this->data[$this->alias]['senha'] = AuthComponent::password($this->data[$this->alias]['senha']);}返回真;}}
僅此而已,無需驗證!所以我的用戶控制器
類 UsersController 擴展了 AppController{公共 $name = "用戶";公共函數 beforeFilter() {父::beforeFilter();$this->Auth->allow('adicionar');}公共函數索引(){}公共功能家(){$this->render('/home');}公共函數登錄(){if($this->request->is('post')){調試($this->請求->數據);死();if($this->Auth->login()){$this->redirect($this->Auth->redirect());}別的{$this->Session->setFlash(__('郵件或密碼錯誤,再試一次!'));}}}公共函數 adicionar(){如果 ($this->request->is('post')) {$this->User->create();if ($this->User->save($this->request->data)) {$this->Session->setFlash(__('用戶已被保存'));$this->redirect(array('action' => 'index'));} 別的 {$this->Session->setFlash(__('用戶無法保存.請再試一次.'));}}}}
怎么看,action login正在渲染視圖Login,有什么問題嗎??
和我的查看登錄進行身份驗證..
Form->create('User',array('url' => array('controller' => 'users', 'action' => '登錄')));echo $this->Form->input('email');echo $this->Form->input('senha');echo $this->Form->submit('Login');echo $this->Form->end();?>
我哪里錯了?我無法驗證..
正在更新....
我怎么說,調試我的項目我可以看到!我的請求數據不為空
但我還是有條件的
調試請求數據
//AppController
App::uses('AuthComponent', 'Controller/Component');類 AppController 擴展控制器 {/*** 組件** @var 數組*/公共 $components = 數組('認證' =>大批('認證' =>大批('表格' =>大批('字段' =>大批('用戶名' =>'電子郵件','密碼' =>'森哈'),)),'登錄重定向' =>數組('控制器' => '用戶','動作' => '家'),'注銷重定向' =>數組('控制器' => '用戶','動作' => '注銷'),'authError' =>'Por 青睞, Fa?a o login para acessar a área Restrita','登錄錯誤' =>請登錄 ou Senha incorreto!"'自動重定向' =>錯誤的);
view - 您必須在創建表單中設置模型名稱 User NOT Users
Form->create('User',array('url' => array('controller' => 'users', 'action' => '登錄'));echo $this->Form->input('email');echo $this->Form->input('senha');echo $this->Form->end();?>
//控制器
公共函數login(){if($this->request->is('post')){if($this->Auth->login()){$this->redirect($this->Auth->redirect());}別的{$this->Session->setFlash('您的用戶名/密碼不正確');}}}
//將此添加到您的用戶模型
App::uses('AppModel', 'Model');/*** 用戶模型*/類用戶擴展 AppModel {公共函數 beforeSave($options = array()) {if (!empty($this->data['User']['password'])) {$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);}返回真;}}
這里是簡單的身份驗證登錄>
i'm starting to cook cake haha, everytime that i submit my form for to make the authentication, nothing happens!
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
));
}
My Model User
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;
}
}
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..
Updating....
How I had said, debuggin my project i can see! My request data is no empty
but i fell conditional else
Debug Request Data
//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
);
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
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;
}
}
here is the Simple Auth login
這篇關于CakePHP:不能做簡單的登錄的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!