CakePHP 3:尝试获取非对象的属性 [英] CakePHP 3 : Trying to get property of non-object

查看:79
本文介绍了CakePHP 3:尝试获取非对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CakePHP 3.2,并且在login()动作中,只允许登录状态为verified = 1

I'm working on CakePHP 3.2 and in login() action, want to allow login to only those whose status is 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]

注意:行号已更改,以满足代码段

NOTE : line number has been changed to meet code snippets

推荐答案

解决了我的问题.这是我更新的login()方法.

Solved my problem. This is my updated login() method.

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'));
      }
    }

这篇关于CakePHP 3:尝试获取非对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆