How to setting global variables in OpenCart 2.3.0.

2019-05-10 01:05发布

问题:

When I want to set a global variable in template (.tpl files), I just use global in opencart 2.2 or older version.

For example:

<?php global $config; if($config->get('my_options')== 1) { ?>....<?php } ?>

but, it does not work in Latest OpenCart 2.3.0.2

Because predefined global $config in controller rather inconvenient (we have use config value in many template file). Hopefully someone with more experience will have an answer to this problem.

回答1:

The least code you will get defining them as constants in config.php or admin/config.php, depending on where you will need them. This will make these constants immediately available throughout the catalog or admin context.

Another way is by using $this->config in controllers to get key/values out of the setting table into your models or controllers.