2017-03-03 1 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_modelAuth

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

내 컨트롤러의 이름을 바꾸지 않고이 충돌을 어떻게 해결할 수 있습니까? 아니면 해당 컨트롤러를 통해 인스턴스화하는 모든 개체에 다른 컨트롤러 이름을 사용해야합니까? 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 
          ); 
      } 
      
    • 당신은 또한 getCredentials 또는 getApiConfig$p을 의미

    • 에 방법 이름을 바꿀 수는 배열이고 당신은 나중에 호출을 확인하기 위해 API를 설정하는 컨트롤러 Psuher의 데이터로 작업 할 수 있습니다

    또 다른 옵션은 컨트롤러와 모델 사이에 서비스 계층을 소개하는 것입니다 :

    • PusherAPIService
    • 제어기 통화
    • controller 인스턴스화 새로운 클래스 model->getCredentials
    • 세트/PusherAPIService에 예를 자격 증명을 전달
    • PusherAPIService에 작업을 추가하십시오. connect(), pull(), push() 서비스가 무엇이든간에. 즉, 서비스 클래스는 외부 API와의 모든 상호 작용을 래핑합니다.
    • 마지막으로 컨트롤러에서 : 필요한 경우 서비스 메소드를 호출하십시오. 예 : 게시물 요청이 들어 오면 컨트롤러가 들어오는 데이터를 처리 한 다음 데이터를 서비스 계층에 주입시킨 다음 지금 구성된 서비스에서 메소드를 호출하여 결과를 컨트롤러로 다시 가져옵니다.
    +1

    와우, 멋진 Jens, 건축에 대한 조언 주셔서 감사합니다. 잠시 동안 나는 컨트롤러 웹 소켓의 이름을 바꾸었지만 구현은 좋은 생각입니다. –

    +0

    다행히 도울 수 있어요 :) –