Authentication CakePHP 3:尝试获取非对象的属性
我正在使用CakePHP 3.2,在Authentication CakePHP 3:尝试获取非对象的属性,authentication,cakephp,cakephp-3.2,Authentication,Cakephp,Cakephp 3.2,我正在使用CakePHP 3.2,在login()操作中,只允许状态为verified=1的用户登录 public function login() { if ($this->request->is('post') || $this->request->query('provider')) { $user = $this->Auth->identify(); if ($user) { if
login()
操作中,只允许状态为verified=1的用户登录
public function login()
{
if ($this->request->is('post') || $this->request->query('provider')) {
$user = $this->Auth->identify();
if ($user) {
if ($user->verified != 1) { // LINE 6
$this->Flash->error(__('You have not yet verified your account. Please check your email to verify your account before login'), [
'params' => [
'userId' => $user->id, // LINE 9
],
['escape' => false]
]);
$this->redirect(['action' => 'login']);
}
$this->Auth->setUser($user);
$this->_setCookie();
$this->Flash->success('Login Success');
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error(__('Invalid username or password, try again'));
}
}
但这也带来了错误
Notice (8): Trying to get property of non-object [APP/Controller/UsersController.php, line 6]
Notice (8): Trying to get property of non-object [APP/Controller/UsersController.php, line 9]
Warning (2): Cannot modify header information - headers already sent by (output started at /var/www/html/argoSystems/projects/project_01/site/vendor/cakephp/cakephp/src/Error/Debugger.php:746) [CORE/src/Network/Session.php, line 572]
Warning (2): session_regenerate_id() [function.session-regenerate-id]: Cannot regenerate session id - headers already sent [CORE/src/Network/Session.php, line 576]
注意:行号已更改以满足代码段的要求
解决了我的问题。这是我更新的login()
方法
public function login()
{
if ($this->request->is('post') || $this->request->query('provider')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
if ($this->Auth->user('verified') != 1) {
$this->Flash->error(__('You have not yet verified your account. Please check your email to verify your account before login'), [
'params' => [
'userId' => $this->Auth->user('id'),
],
['escape' => false]
]);
return $this->redirect($this->Auth->logout());
}
$this->_setCookie();
$this->Flash->success('Login Success');
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error(__('Invalid username or password, try again'));
}
}
解决了我的问题。这是我更新的login()
方法
public function login()
{
if ($this->request->is('post') || $this->request->query('provider')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
if ($this->Auth->user('verified') != 1) {
$this->Flash->error(__('You have not yet verified your account. Please check your email to verify your account before login'), [
'params' => [
'userId' => $this->Auth->user('id'),
],
['escape' => false]
]);
return $this->redirect($this->Auth->logout());
}
$this->_setCookie();
$this->Flash->success('Login Success');
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error(__('Invalid username or password, try again'));
}
}
你本可以轻松解决这个问题的!例如,将第6行更改为
如果($user->verified!=1){//第6行
到
$user[“verified”!=1){//第6行
您本可以轻松解决此问题!例如,将第6行从
如果($user->verified!=1){//第6行
到
$user[“已验证”]!=1){//第6行
您是否更改了错误通知的行以匹配您的代码段?如果您这样做,请告诉人们,否则他们可能会忽略您,因为找出行号可能指向的位置是一项相当繁琐的任务。尽管如此,您确实需要开始更多地学习PHP和调试由于这是一个非常非常琐碎的问题,所以对基础知识进行分析,并找出其原因非常简单,只需阅读AuthComponent::identify()的API文档即可
,并调试$user
到底是什么。谢谢@ndm debugging$user提供了正确的参数和值。您是否更改了错误通知的行以匹配您的代码段?如果您这样做,请告诉人们,否则他们可能会忽略您,因为找出行号可能指向的位置是一个pr尽管如此,您确实需要开始学习更多关于PHP和调试基础知识的知识,因为这是一个非常非常琐碎的问题,找出它发生的原因就像阅读AuthComponent::Identifity()的API文档一样简单
,并调试$user
到底是什么。感谢@ndm debug$user提供正确的参数和值。