2013-02-28 39 views
0

重新映射我有下面的代碼,我加入到正由我的所有控制器擴展一個MY_Controller:處理笨與參數

public function _remap($method, $params = array()) 
    {//exit($this->router->fetch_class()); 
     if (array_search($method, $this->private_methods) !== false && !$this->logged_in) 
     { 
      $this->session->set_flashdata('message', array(
                 'message' => 'You must login to access the requested area', 
                 'error'  => 1 
                 ) 
             ); 
      redirect('/'); 
     } 
     else if (method_exists($this, $method)) 
     { 
      $this->$method($params); 
     } 
     else 
     { 
      redirect('/'); 
     } 

    } 

正在創建的問題是,呼叫$this->$method($params)凝聚着PARAMS中的陣列。所以如下面休息方法:

function some_method($param1, $param2, $param3) 

有沒有辦法打破這種陣列回個別項目像這樣的功能呢?

+0

這是我的帖子,上面的代碼是基於該代碼。您所指的鏈接中的代碼不會分開參數,而是將它們作爲ARRAY發送。 – 2013-02-28 03:51:20

+0

你可能做錯了什麼。我測試過它正在發送個別參數。創建'Example'控制器並測試答案。 – 2013-02-28 04:10:59

回答

0

我試圖用

$this->$method($params); 

做同樣的,找到同樣的問題,我發現了另一個選項

call_user_method_array($method,$this,$params); 

哪些工作,但已經過時。 Function in PHP deprecated, what should I use now?

但希望這是新的。

call_user_func_array(array($this,$method), $params); 
0

我已經嘗試這樣做,它包含參數的方法的名字爲我工作

public function _remap($method,$params = array()) 
    { 
    if (method_exists($this, $method)) { 

     if($method == 'delete_photo' || $method == 'delete_video'){ 

     call_user_func_array(array($this,$method), $params); 
     } 

     else{ 
     $this->$method(); 
     } 
     } 
     else { 
     $this->index($method); 
    } 
    } 

現在,你只需要替換「delete_photo」 &「delete_video」,當然你可以添加儘可能多的方法,你想要的。