Accessing class properties and methods from templa

2019-06-06 07:35发布

Prior to Opencart 2.0 class properties could be accessed directly from the template. For example, $this->config->get('config_language') or $this->request->get['route'] were both accessible from an admin template file.

With the new method $this->load->view(), neither of these work. Is there a simple way to pass on class methods and properties which are available in the current controller to the tpl without explicitly adding them to the $data array?

1条回答
欢心
2楼-- · 2019-06-06 07:37

In Opencart version 2, if you want these variables then you can easily access them. There is slight change is code, now you can use

$this->registry 

which holds everything. So you have to get these things form

$this->registry

like this

$this->registry->get('config')

it will work like

$this->config  

so your

$this->config->get('config_language')

becomes

$this->registry->get('config')->get('config_language')

like this

$this->request->get['route'] == $this->registry->get('request')->get['route'];

$this->request->post['route'] == $this->registry->get('request')->post['route'];

$this->request->files['file'] == $this->registry->get('request')->files['file'];

for more just simply print $this->registry in any template.

查看更多
登录 后发表回答