简体   繁体   中英

Opencart: How To Pass Data Variable From Parent To Child Controller

Is there any simple solution to pass $this->data variable from parent controller to all children controllers.

All what I try bring empty array to child.

When I change the file "system/engine/controller.php" like below:

protected function getChild($child, $args = array()) {
    $action = new Action($child, $args);

    if (file_exists($action->getFile())) {
        require_once($action->getFile());

        $class = $action->getClass();

        $controller = new $class($this->registry);

        //$controller->{$action->getMethod()}($action->getArgs());
        $controller->{$action->getMethod()}($action->getArgs()+array('parent-data'=>$this->data));

        return $controller->output;
    } else {
        trigger_error('Error: Could not load controller ' . $child . '!');
        exit();                 
    }       
}

Then I try to read the variable 'parent-data' from the passed arguments in the child controller:

if (isset($setting['parent-data'])) {
    echo "<pre>".print_R($setting['parent-data'],true)."</pre>";
}

As a result I get an empty array:

Array
(
    [modules] => Array
       (
        )
)

The data variable is empty. Thats why it prints blank array. Also there is no need to pass the data varaiable.Its a global one and you will get it till upto the .tpl files.

问题出在父级,它是具有空数据变量的列控制器,而不是具有必要数据变量的主页父级控制器。

on opencart2 and maybe older:

ex: parse ver (steps) from parent controller product-custom to child controller header

parent

<?php
class ControllerProductCustom extends Controller {
    private $error = array();

    public function index() {
        $this->load->language('product/product');

...

            $data['footer'] = $this->load->controller('common/footer');
            $data['header'] = $this->load->controller('common/header', array('steps' => true));

child

<?php
class ControllerCommonHeader extends Controller {
    public function index($arg) {
        $data['title'] = $this->document->getTitle();
        if (isset($arg['steps'])) $data['steps'] = $arg['steps'];
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM