2014-09-26 03:13
浏览 43


I am currently developing OpenCart extensions. I am looking through existing extensions to understand the architecture (and familiarizing MVC/PHPOO concepts).

Frequently, this line of code comes up:

$seller_email = $this->config->get('service_seller_email');

My interpretation based on reading this thread:

I understand that this is a variable assignment, where it's accessing a model (?) called config to retrieve a string called service_seller_email from the admin settings portal. However, I have tried navigating through the various MVC folders within OpenCart, and I could not find a specific config.php. Could you please correct my interpretation if it's wrong?

EDIT: To add to the confusion, the article I linked specifies that you need to load a config in a controller before using it, like $this->language->load('product/search');, but I cannot find a line such as $this->config->load('...'); or the like.

图片转代码服务由CSDN问答提供 功能建议

我目前正在开发OpenCart扩展。 我正在通过现有的扩展来了解架构(并熟悉MVC / PHPOO概念)。


$ seller_email = $ this-> config-> get('service_seller_email');


我明白了 这是一个变量赋值,它正在访问一个名为 config 的模型(?),以从管理设置门户中检索名为 service_seller_email 的字符串。 但是,我尝试浏览OpenCart中的各种MVC文件夹,但我找不到特定的 config.php 。 如果错误的话,请你纠正我的解释吗?

编辑:为了增加混乱,我链接的文章规定你需要加载一个 在使用之前在控制器中配置,例如 $ this-> language-> load('product / search'); ,但我找不到像这样的行 $ this-> config-> load('...'); 等。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanmeng9336 2014-09-26 07:10

    Your interpretation is incorrect. The config class is not considered as model (despite it is accessing DB directly) but as a system library class - that's why you can find it under system/library/ folder.

    OpenCart has it's implementation of service locator (registry) which is used to to store the config object so that you can access it directly from anywhere (inside of OpenCart of course).

    It is loaded in both index.php files (in root and under admin/) like this:

    $config = new Config();
    $registry->set('config', $config);

    This $registry is our service locator and it is passed over to any controller in it's __construct() method. Each controller has also a magic method __get() which is responsible for retrieving objects from this $registry - take a look at abstract Controller class at system/engine/controller.php which each controller extends (and should extend).

    That's why you can freely call directly


    The first part, $this->config will fall into the __get() method passing over the 'config' value as a $key which in turn is passed over to next call

    return $this->registry->get($key);

    And since the config object is registered under 'config' key in our service locator, it is retrieved and returned so that we could call get('some_config_key') on it.

    Now the some_config_key key is stored in DB in setting table and the config object tries to find it and get it's value (you can take a look at how this works in system/library/config.php).

    Hopefully this clarifies your confusion.

    解决 无用
    打赏 举报
  • doutian3269 2014-09-26 03:43

    Looking at the docs, it looks like the config values are stored in the setting table. The Config class itself is located in /system/library/config.php.

    解决 无用
    打赏 举报
  • doujiao6116 2014-09-26 04:39

    In admin > index.php you wil find

    // Config
    $config = new Config();
    $registry->set('config', $config);

    That's where it is initialized. The class is located in

    system > library > config.php

    Config loaded in system > engine >loader.php:

    public function config($config) {
    解决 无用
    打赏 举报

相关推荐 更多相似问题