【问题标题】:CakePHP Fatal error: Call to a member function check() on a non-object?CakePHP 致命错误:在非对象上调用成员函数 check()?
【发布时间】:2017-07-20 08:50:18
【问题描述】:

大家好,我遇到了这个错误

"致命错误:在非对象上调用成员函数 check() C:\xampp\htdocs\job_portal_cakephp\app\Controller\Component\SeekerSessionComponent.php 第 4 行"

组件代码(SeekerSessionComponent.php)

<?php
class SeekerSessionComponent extends Component{
    function session_check(){
        if(!$this->Session->check("id")){
            die;
            $this->redirect(array("controller"=>"Pages","action"=>"login"));
        }
    }
}
?>

控制器代码(PagesController.php)

App::uses('AppController', 'Controller');


class PagesController extends AppController {
public $name = 'Pages';


public $helpers = array('Html', 'Session');


public $uses = array("Job","Page","Seeker","Skill");

public $components = array("Sanitize","SeekerSession");

public function index(){
    $this->SeekerSession->session_check();
    $this->layout = "first_layout";
    $jobs = $this->Job->query();
    $this->set(compact("jobs"));
}
}

我有 Pages 控制器,它具有索引功能,它使用 SeekerSessionComponent 来检查是否存在带有变量“id”的会话。

【问题讨论】:

标签: php session cakephp


【解决方案1】:

基于链接:-https://book.cakephp.org/2.0/en/controllers/components.html#creating-a-component

您忘记在组件类代码中添加App::uses(Component, Controller);。所以它应该如下所示:-

App::uses('Component', 'Controller');//missed

class SeekerSessionComponent extends Component {
   public $components = array('Session');// missed
    public function session_check(){
        if($this->Session->check("id")){ // if id exist
            return true; //return true
        }
    }
}

注意:-

组件函数必须是public

还有

die;$this-&gt;redirect(array("controller"=&gt;"Pages","action"=&gt;"login")); 似乎代码不正确,您需要return 一些东西不会停止执行或重定向到任何页面。

【讨论】:

    【解决方案2】:

    通过在上述代码的第 4 行打印 $this->Session 进行检查。这返回一个空值意味着它不返回任何对象,然后您的代码尝试在 null 上调用 check() 函数。

     <?php
        class SeekerSessionComponent extends Component{
            function session_check(){
                echo "<pre>";
                print_r($this->Session);
                die;
                /*if(!$this->Session->check("id")){
                    die;
                    $this->redirect(array("controller"=>"Pages","action"=>"login"));
                }*/
            }
        }
        ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-16
      • 1970-01-01
      • 1970-01-01
      • 2013-11-25
      相关资源
      最近更新 更多