2017-03-01 2 views
0

ねえ、誰もが、私はこのエラーを取得していますCakePHP致命的なエラー:非オブジェクト上のメンバ関数check()を呼び出しますか?

"Fatal error: Call to a member function check() on a non-object in C:\xampp\htdocs\job_portal_cakephp\app\Controller\Component\SeekerSessionComponent.php on line 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")); 
} 
} 

私が持っているページのコントローラを持っていますSeekerSessionComponentを使用して、変数 "id"とのセッションが存在するかどうかをチェックするインデックス関数。リンクに基づく

+0

この関数が定義するクラスを確認しますか? –

+0

'App :: uses(Component、Controller); 'はあなたのコンポーネントクラスコードでは見逃されています: - https://book.cakephp.org/2.0/en/controllers/components.html#creating-a-component and function must 「公然」である –

答えて

1

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

あなたはそれがあるべきcode.So以下のようなあなたのコンポーネントクラスに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ものにする必要があります。

0

上記のコードの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")); 
      }*/ 
     } 
    } 
    ?> 
関連する問題