繁体   English   中英

CakePHP致命错误:在非对象上调用成员函数check()?

[英]CakePHP Fatal error: Call to a member function check() on a non-object?

大家好,我收到此错误

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

组件代码(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”的会话。

基于链接: -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->redirect(array("controller"=>"Pages","action"=>"login")); 似乎没有正确的代码,您需要return一些内容,而不是停止执行或重定向到任何页面。

通过在上述代码的第4行中打印$ this-> Session进行检查。 这将返回一个null值,意味着它不返回任何对象,然后您的代码尝试在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"));
            }*/
        }
    }
    ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM