我是 cakephp 新手,直接开始学习 cakephp3.2 我在自定义控制器中创建了一个函数,如下所示。
public function login(){
if($this->request->is('post')){
$user = $this->auth->identify();
if($user){
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
}else{
$this->Flash->error(__('Username or password is incorrect'), [
'key' => 'auth'
]);
}
}
}
class AppController extends Controller
{
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth',[
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
]
]
],
'authError' => 'Did you really think you are allowed to see that?',
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
],
'storage'=>'Session'
]);
$this->Auth->allow(['display']);
}
public function beforeRender(Event $event)
{
if (!array_key_exists('_serialize', $this->viewVars) &&
in_array($this->response->type(), ['application/json', 'application/xml'])
) {
$this->set('_serialize', true);
}
}
}
我不明白如何处理它。我应该加载任何外部组件或帮助程序吗?