2017-03-03 10 views
1

問題が発生しましたが、解決方法がわかりません。Codeigniterコントローラ名とオブジェクト名の衝突

私は私が私のコントローラで返さプッシャーオブジェクトを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 
     ); 
    } 
} 

をインスタンス化する方法でモデルPusher_modelを持っている方法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を持っています...

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* 
    } 
} 

...結果として全体コントローラ「プッシャー」を取得します。

Q:

がどのように私は私のコントローラの名前を変更することなく、この衝突を解決するか、私はそのコントローラを介してインスタンス化する任意のオブジェクトに異なるコントローラ名を使用しなければならないのですか?

答えて

1

コントローラの名前を変更せずにこの衝突を解決するにはどうすればよいですか、コントローラを介してインスタンス化するオブジェクトに別のコントローラ名を使用する必要がありますか? [OK]を

、コントローラが呼ばれたのは...

  • を見てみましょう:コントローラの負荷Pusher->auth()
  • をし、モデルを呼び出す:$this->Pusher_model->newPusher();
  • 、あなたはreturn new Pusherを使用して新しいプッシャーオブジェクトをインスタンス化していますコントローラを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 
        ); 
    } 
    
  • あなたもgetCredentialsまたはgetApiConfig$pを意味

  • にメソッドの名前を変更する可能性が配列され、後でそれへの呼び出しを作るためにあなたのAPIを設定するには、コントローラPsuherにデータを操作することができます

別のオプションは、コントローラとモデルの間でサービス層を導入することである。

  • セット/例えば、PusherAPIServiceに資格情報を渡すコントローラコールでcontroller
  • でインスタンス
    • 新しいクラスPusherAPIService
    • configure($credentials)
    • PusherAPIServiceにアクションを追加します。 connect()pull()push()あなたのサービスが何であれ。これは、サービスクラスが外部APIとのすべての対話をラップすることを意味します。
    • 最後に、私たちのコントローラーで:必要に応じてサービスメソッドを呼び出します。例えば投稿要求が到着したら、コントローラーに着信データを処理させてから、サービスレイヤーにデータを注入し、現在設定されているサービスのメソッドを呼び出して結果をコントローラーに戻します。
  • +1

    うわー、素敵な一人ジェーンズ、建築に関するヒントありがとう、私はそれほど教育的だと分かっています!現時点で私はコントローラのWebソケットの名前を変更しましたが、あなたの実装は良い考えです。 –

    +0

    うれしい私は助けることができます:) –

    関連する問題