Codeigniter控制器名称和对象名称冲突

时间:2017-03-03 12:53:29

标签: php codeigniter oop controller composer-php

我遇到了问题,我不知道如何解决它。

我有一个控件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,其中包含一个实例化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()我的控制器中返回的Pusher对象时......

var_dump($p)

我得到了整个控制器' Pusher'结果......

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 个答案:

答案 0 :(得分:1)

  

如何在不重命名控制器的情况下解决此冲突,或者是否必须对通过该控制器实例化的任何对象使用不同的控制器名称?

好的,我们看看..

  • 控制器被调用:Pusher->auth()
  • 控制器加载并调用模型:$this->Pusher_model->newPusher();
  • 然后使用return new Pusher实例化一个新的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
        );
    }
    
  • 您还可以将方法重命名为getCredentialsgetApiConfig

  • 表示$p是一个数组,您可以使用控制器Psuher中的数据来设置API,以便稍后调用它

另一种选择是在控制器和模型之间引入服务层:

  • 新课程PusherAPIService
  • controller
  • 中实例化 控制器调用中的
  • model->getCredentials
  • 将凭据设置/传递给PusherAPIService,例如configure($credentials)
  • PusherAPIService添加操作,例如connect()pull()push()无论您的服务是什么。这意味着服务类包装了与外部API的所有交互。
  • 然后终于在我们的控制器中:在需要时调用服务方法。例如当一个post请求进来时,让控制器处理传入的数据,然后将数据注入服务层,然后在现在配置的服务上调用一个方法,将结果返回给控制器。