2017-03-01 58 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和function must被「公開」 –

回答

1

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

你忘了在你的組件類添加App::uses(Component, Controller); code.So應該象下面這樣: -

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

通過打印$ this-> Session來檢查上述代碼的第4行。這返回一個空值意味着它不返回任何對象,然後你的代碼嘗試調用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")); 
      }*/ 
     } 
    } 
    ?> 
相關問題