通过控制器中的功能加载视图


loading a view via function in controller

当在opencart中选择产品时,我正在尝试获取选项卡上的尺寸图。为此,我有一个像下面这样的模型

class ModelCatalogSizes extends Model {     
    public function getSizes()
    {
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "sizes ORDER BY id");
        return $query->rows;
    }
}

我将视图作为相同的模板,它只是一个html表,具有作为输入单选选项的值。

在我的产品控制器类中,我有一个类似的函数

public function sizes()
{
    $this->language->load('product/product');
    $this->load->model('catalog/sizes');
    $this->data['sizes'] = array();
    $results = $this->model_catalog_sizes->getSizes();
    foreach ($results as $result) {
        $this->data['sizes'][] = array(
            'type'       => ucfirst($result['type']),
            'coat'       => $result['coat'],
            'chest'      => $result['chest'],
            'cverarm'    => $result['overarm'],
            'waist'      => $result['waist'],
            'hip'        => $result['hip'],
            'inseam'     => $result['inseam'],
            'neck'       => $result['neck'],
            'sleeve'     => $result['sleeve'],
            'height'     => $result['height'],
            'weightLb'   => $result['weightLb'],
            'weightKg'   => $result['weightKg'],
        );
    }
    if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/sizes.tpl')) {
        $this->template = $this->config->get('config_template') . '/template/product/sizes.tpl';
    } else {
        $this->template = 'default/template/product/sizes.tpl';
    }
    $this->response->setOutput($this->render());
}

现在,我正试图通过产品控制器类中的索引函数直接加载此视图,方法是说$this->data['size'] = $this->sizes();

当我在产品视图中回显我的$size时,不会出现任何内容。我认为上面函数中构建的整个视图应该显示出来。我错了吗(概率99%)?有人能帮我通过函数直接发布视图吗?

您需要做的是将其路由添加为index()方法的子级的子级打开/catalog/controller/product/product.php,在index()方法的底部找到此代码

$this->children = array(
    'common/column_left',
    'common/column_right',
    'common/content_top',
    'common/content_bottom',
    'common/footer',
    'common/header'
);

并添加您的路线,对于您的sizes()方法,该路线将是'product/product/sizes'

$this->children = array(
    'common/column_left',
    'common/column_right',
    'common/content_top',
    'common/content_bottom',
    'common/footer',
    'common/header',
    'product/product/sizes'
);

然后在你的模板中,你只需要使用<?php echo $sizes; ?>,无论你想让它去哪里