2017-03-03 155 views
1

我遇到了一個問題,我不知道如何解決它。Codeigniter控制器名稱和對象名稱衝突

我有一個控制器Pusher與方法Auth

class Pusher extends MX_Controller 
{ 
    /** 
    * 
    */ 
    public function auth() 
    { 
     $this->load->model('pusher/Pusher_model'); 
     $p = $this->Pusher_model->newPusher(); 
     var_dump($p);die; 
    } 
} 

我有一個方法的模型Pusher_model當我var_dump()返回推對象在我的控制器實例化new Pusher()對象

require 'vendor/autoload.php'; 

class Pusher_model extends CI_Model 
{ 
    public $options; 
    /** 
    * 
    */ 
    public function __construct() 
    { 
     $this->config->load('api_config'); 
     $this->options = array(
      $this->config->item('pusher_cluster'), 
      $this->config->item('pusher_is_encrypted') 
     ); 

    } 

    /** 
    * 
    */ 
    public function newPusher() 
    { 
     return new Pusher(
      $this->config->item('pusher_public_key'), 
      $this->config->item('pusher_secret_key'), 
      $this->config->item('pusher_api_id'), 
      $this->options 
     ); 
    } 
} 

...

var_dump($p) 

我得到整個控制器「推」,結果......

object(Pusher)#24 (2) { 
    ["autoload"]=> 
    array(0) { 
    } 
    ["load"]=> 
    object(MY_Loader)#25 (13) { 
    ["_module":protected]=> 
    string(6) "pusher" 
    ["_ci_plugins"]=> 
    array(0) { 
    } 
    ["_ci_cached_vars"]=> 
    &array(0) { 
    } 
    ["_ci_ob_level":protected]=> 
    int(1) 
    ["_ci_view_paths":protected]=> 
    &array(1) { 
     ["C:\xampp\htdocs\php\twocan\twocan_beta\App\views\"]=> 
     bool(true) 
    } 
    ["_ci_library_paths":protected]=> 
    &array(2) { 
     [0]=> 
     string(43) "C:\xampp\htdocs\php\twocan\twocan_beta\App\" 
     [1]=> 
     string(43) "C:\xampp\htdocs\php\twocan\twocan_beta\Sys\" 
    } 
    ["_ci_model_paths":protected]=> 
    &array(2) { 
     [0]=> 
     string(58) "C:\xampp\htdocs\php\twocan\twocan_beta\App\modules/pusher/" 
     [1]=> 
     string(43) "C:\xampp\htdocs\php\twocan\twocan_beta\App\" 
    } 
    ["_ci_helper_paths":protected]=> 
    &array(2) { 
     [0]=> 
     string(43) "C:\xampp\htdocs\php\twocan\twocan_beta\App\" 
     [1]=> 
     string(43) "C:\xampp\htdocs\php\twocan\twocan_beta\Sys\" 
    } 
    ["_ci_classes":protected]=> 
    &array(15) { 
     ["benchmark"]=> 
     string(9) "Benchmark" 
     ["hooks"]=> 
     string(5) "Hooks" 
     ["config"]=> 
     string(6) "Config" 
     ["log"]=> 
     string(3) "Log" 
     ["utf8"]=> 
     string(4) "Utf8" 
     ["uri"]=> 
     string(3) "URI" 
     ["router"]=> 
     string(6) "Router" 
     ["output"]=> 
     string(6) "Output" 
     ["security"]=> 
     string(8) "Security" 
     ["input"]=> 
     string(5) "Input" 
     ["lang"]=> 
     string(4) "Lang" 
     ["loader"]=> 
     string(6) "Loader" 
     ["session"]=> 
     string(7) "Session" 
     ["form_validation"]=> 
     string(15) "Form_validation" 
     ["model"]=> 
     string(5) "Model" 
    } 
    ["_ci_models":protected]=> 
    &array(1) { 
     [0]=> 
     string(12) "Pusher_model" 
    } 
    ["_ci_helpers":protected]=> 
    &array(5) { 
     ["url_helper"]=> 
     bool(true) 
     ["html_helper"]=> 
     bool(true) 
     ["security_helper"]=> 
     bool(true) 
     ["language_helper"]=> 
     bool(true) 
     ["form_helper"]=> 
     bool(true) 
    } 
    ["_ci_varmap":protected]=> 
    &array(2) { 
     ["unit_test"]=> 
     string(4) "unit" 
     ["user_agent"]=> 
     string(5) "agent" 
    } 
    ["controller"]=> 
    *RECURSION* 
    } 
} 

這使我相信有一個名字碰撞實例化對象和控制器本身的事情。

問:

如何解決此衝突而不是重命名我的控制器或做我必須使用不同的控制器名稱,我通過該控制器的實例任何對象?

回答

1

如何解決此衝突而無需重命名我的控制器,還是必須使用不同的控制器名稱來通過該控制器實例化的任何對象?

好吧,讓我們來看看..

  • 控制器被稱爲:Pusher->auth()
  • 控制器負載和調用模型:使用return new Pusher$this->Pusher_model->newPusher();
  • 那麼你實例化一個新的推對象並將其返回到Pusher控制器。
  • 這意味着你的Pusher控制器有一個屬性$p包含一個新的推動器對象
  • 是的,你在這裏的圈子。

我會建議保持簡單:

  • 不要實例new Pusher,而是簡單地從模型(Pusher_model)控制器,例如返回連接數據

    public function newPusher() 
    { 
        return array(
         $this->config->item('pusher_public_key'), 
         $this->config->item('pusher_secret_key'), 
         $this->config->item('pusher_api_id'), 
         $this->options 
        ); 
    } 
    
  • ,你還可以將方法重命名爲getCredentialsgetApiConfig

  • 這意味着$p是一個數組,您可以用數據控制器Psuher工作,以建立您的API以後對其進行的呼叫

另一種選擇是控制器和模型之間引入一個服務層:

  • 新類PusherAPIService
  • 在控制器呼叫實例在controller
  • model->getCredentials
  • 集合/傳遞憑證到PusherAPIService,例如configure($credentials)
  • 將動作添加到PusherAPIService,例如, connect()pull(),push()無論您的服務如何。這意味着服務類會封裝與外部API的所有交互。
  • 然後終於在我們的控制器中:當您需要它們時調用服務方法。例如當請求發佈時,讓控制器處理傳入的數據,然後將數據注入服務層,然後調用現在配置的服務上的方法,將結果返回給控制器。
+1

哇,很好的Jens,謝謝你的建築技巧,我覺得這麼教育!目前我重命名我的控制器Websockets,但是你的實現是一個好主意。 –

+0

很高興我能幫忙:) –