我正在使用Cake的Auth组件,在使用scope
时似乎无法弄清楚如何设置特定的Flash数据/错误消息。
通过将active
从0
更改为1
进行测试,我可以确认scope
参数有效,但是如果scope
返回false,我会得到闪存与我的login
方法Your username or password was incorrect.
相关联的数据。
UsersController
public function login(){
if($this->request->is('post')){
if($this->Auth->login()){
$this->Session->setFlash('You are logged in!');
return $this->redirect($this->Auth->redirect());
}
$this->Session->setFlash(__('Your username or password was incorrect.'));
}
}
AppController的
public $components = array(
'DebugKit.Toolbar',
'Acl',
'Auth' => array(
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
),
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email'),
'scope' => array('active' => '1')
)
)
),
'Session'
);
public function beforeFilter() {
$this->Auth->loginAction = array(
'controller' => 'Users',
'action' => 'login'
);
$this->Auth->logoutRedirect = array(
'controller' => 'Users',
'action' => 'login'
);
$this->Auth->loginRedirect = array(
'controller' => 'Users',
'action' => 'index'
);
}
是否可以为每个scope
参数和login
方法绑定特定的错误消息?
答案 0 :(得分:1)
在CakePHP中非常简单:) 您可以在Controller的每个方法中自定义错误消息 很简单,就像我说的那样^ _ ^
Exmple:
<?php
public function accessSite(){
//disable default message authError of Auth
$this->Auth->authError = false;
$message = 'You not have permission access here';
//set new custom message
$this->Auth->flash($message);
}