2013-05-28 26 views
1

我想通過單獨的AJAX調用使用Codeigniter編寫的同一控制器中調用不同的方法,並在其中一個調用中創建第三個庫的對象,並稍後訪問同一個對象(也自動加載Session庫)在第二個方法,同時使第二AJAX調用:在Codeigniter中通過ajax調用對象引用

JavaScript有2個獨立的AJAX調用:

 $.post('http://mysite.com/UserController/controllerA',{Param1:x,Param2:y}); 
    $.post('http://mysite.com/UserController/controllerB'); 

而且控制器的樣子:

 require_once 'FILEPATH/Thirdlibrary.php';   
    class UserController extends CI_Controller { 

    protected $service; 

    public controllerA{ 
     $varA = $this->input->post('Param1'); 
     $varB = $this->input->post('Param2'); 

     $this->service = new ThirdLibray($varA,$varB); 

     $this->service->startCall();//Thirdlibrary has method startCall() and stopCall 
    } 

    public controllerB{ 
     //I want to refer to the same object $this->service here 

     $this->service->stopCall(); 
    } 

現在我知道,PHP每次重新初始化的對象其加載/訪問,我怎麼能做出這樣work.How到ovecome這樣的錯誤:

Call to a member function stopCall() on a non-object 

(使所有的努力後,搜索查詢這裏編碼)

+0

如何使用'flashdata'或'sessions' – tomexsans

+0

把'$ this-> service = new ThirdLibray($ varA,$ varB);'in'__construct()'需要注意的一點是, '$ varA'和'$ varB'。不考慮上下文,提供解決方案並不容易。 – itachi

+0

itachi放置'$ this-> service = new ThirdLibray($ varA,$ varB);'__construct()'中的問題是,每次當您在codeigniter中通過AJAX調用控制器方法時,構造函數都會獲取調用並最終創建一個新對象'$ this-> service = new ThirdLibray($ varA,$ varB);'每次。我早些時候嘗試過。 – AmateurP

回答

0

將對象存儲到會話中並使用它。

require_once 'FILEPATH/Thirdlibrary.php';   
class UserController extends CI_Controller { 

protected $service; 

public controllerA{ 
    $varA = $this->input->post('Param1'); 
    $varB = $this->input->post('Param2'); 

    if($this->session->userdata('lib_service')) 
    { 
     $this->service =$this->session->userdata('lib_service'); 
    } 
    else 
    { 
     $this->service = new ThirdLibray($varA,$varB); 
    } 
    $this->service->startCall();//Thirdlibrary has method startCall() and stopCall 
    $this->session->set_userdata('lib_service',$this->service); // Why i am saving the object here?, startCall may update some object properties, so that-also will be saved. Otherwise you can have this line in the else part above, after object creation. 
} 

public controllerB{ 
    //I want to refer to the same object $this->service here 
    if($this->session->userdata('lib_service')) 
    { 
     $this->service =$this->session->userdata('lib_service'); 
     $this->service->stopCall(); 
     // Here after stop if you don't want the object, you can clear from session. 
    } 

} 
+0

這也沒有幫助,我曾經嘗試過,但嘗試過一次更多的是你建議的。所以我仍然遇到錯誤:調用一個非對象的成員函數stopCall()。所以,我試圖檢查'$ this-> service'是否獲得了相同的對象,爲此我在'$ this-> session-> set_userdata('lib_service')之前的ControllerA中執行了'var_dump($ this-> service) ',serialize($ this-> service));'在$ this-> service = unserialize($ this-> session-> userdata('lib_service'));'後面的ControllerB中。在ControllerB中,我將'boolean false'作爲響應。 :| – AmateurP

+0

@AmateurP。請嘗試不使用序列化和反序列化 –

0

你可以嘗試使用單例模式爲PHP。這是爲您的情況實施它的基本代碼,但我不確定它適用於您。讓我們嘗試:

  1. 首先創建一個wrraped類的第三方庫:ThirdlibraryWrapped.php,你可以看到更多的單身Best practice on PHP singleton classes

    類ThirdLibraryWrapped {

    public ThirdLibray thirdLibrary; 
    
    public static function Instance($param1=null,$param2 = null) 
    { 
        static $inst = null; 
        if ($inst === null) { 
         $inst = new ThirdlibraryWrapped($param1,$param2); 
        } 
        return $inst; 
    } 
    
    private function __construct($param1,$param2) 
    { 
        if($param1!=null && $param2 != null) 
          thirdLibrary = new ThirdLibrary($param1,$param2); 
        else 
          //default constructor of thirdLibrary 
          thirdLibrary = new ThirdLibrary(); 
    }} 
    
  2. 爲您控制器代碼:

    require_once 'FILEPATH/ThirdlibraryWrapped.php';   
    class UserController extends CI_Controller { 
    protected $service; 
    public controllerA{ 
        $varA = $this->input->post('Param1'); 
        $varB = $this->input->post('Param2'); 
    
        $this->service = ThirdlibraryWrapped::Instance($varA,$varB); 
    
        $this->service->thirdLibrary->startCall();//Thirdlibrary has method startCall() and stopCall 
    } 
    
    public controllerB{ 
        //I want to refer to the same object $this->service here 
        $this->service = ThirdlibraryWrapped::Instance(); 
        $this->service->thirdLibrary->stopCall(); 
    }}