5

我在向 Opencart2 添加管理页面时遇到问题,并且在 SO 上遵循几乎相同问题的答案并没有帮助,所以我相信这个问题是 OC2 特有的。

这个问题的答案之后,我仍然收到错误消息“致命错误:在第 13 行的 C:\websites\weddingshoponline\shop\admin\controller\custom\helloworld.php 中调用未定义的方法 ControllerCustomHelloWorld::render()。任何帮助将不胜感激,因为我一直在转圈。

谢谢你。

PS 恢复到以前版本的 OC 不是有效的响应,尽管是一个很好的响应。

4

2 回答 2

19

在 OC < 2.0 和 OC 2.0 中的页面渲染之间的区别只有很少,但您必须注意它们。

1.$data

在 OC < 2.0 你会这样做:

$this->data['text_button_save'] = $this->language->get('text_button_save');

而在 OC 2.0 中它只是$data,即

$data['text_button_save'] = $this->language->get('text_button_save');

$this->load->view()作为参数传递给方法,例如:

$this->response->setOutput($this->load->view('catalog/category_list.tpl', $data));

2.$this->render()

离开了。现在你正在打电话$this->load->view('catalog/category_list.tpl', $data)

3.$this->children

离开了。现在模板子模块的位置被实例化为模板属性的一部分,而您必须手动调用它们的控制器(为什么?):

$data['header'] = $this->load->controller('common/header');
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');

我在想,到底为什么需要这些改变。改进了什么?他们是否希望开发人员编写更少的代码?现在是不是更遵循 OOP、MVC、WTF(对不起)原则了?并得到了答案:(或第一个没有)。

我们仍然需要加载翻译(我的意思是,我们仍然需要加载每个单独的字符串翻译)。并且gettext已经存在超过 8 年...

$this->response->setOutput($this->render());我们现在不得不调用更长的时间(而且难以理解),而不是简短的$this->response->setOutput($this->load->view('catalog/category_form.tpl', $data));。为什么我们不能这样做:$this->render('catalog/category_form.tpl', $data);???

我个人认为 OC 2.0 和以前一样从开发人员的角度来看)。他们只是改变了包装。但是,老实说,那里有更大的排泄物,这就是我坚持使用 OpenCart 的原因 :-)

于 2014-10-24T08:00:37.293 回答
5

详细说明 shadyyx 对这个问题的回答,以及我开始工作的代码......我并不是说它是完美的,只是它有效。

管理员\控制器\自定义\helloworld.php

<?php
class ControllerCustomHelloWorld extends Controller
{
    private $error = array();
    public function index()
    {
        $this->load->model('setting/setting');
        $this->load->language('custom/helloworld');

        $data['breadcrumbs'] = array();
        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
        );

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_module'),
            'href' => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL')
        );

        $data['heading_title'] = $this->language->get('heading_title');
        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');        
        $this->response->setOutput($this->load->view('custom/helloworld.tpl', $data));
    }
}
?>

admin\language\english\custom\helloworld.php

<?php
// Heading
$_['heading_title'] = 'My First Admin Page...';
    // Text
    $_['text_module']         = 'Modules';
    $_['text_success']        = 'Success: You have modified module account!';
    $_['text_content_top']    = 'Content Top';
    $_['text_content_bottom'] = 'Content Bottom';
    $_['text_column_left']    = 'Column Left';
    $_['text_column_right']   = 'Column Right';
    // Entry
    $_['entry_layout']        = 'Layout:';
    $_['entry_position']      = 'Position:';
    $_['entry_status']        = 'Status:';
    $_['entry_sort_order']    = 'Sort Order:';
    // Error
    $_['error_permission']    = 'Warning: You do not have permission to modify module account!';
    ?>

管理员\模型\自定义\helloworld.php

<?php
class ModelCustomHelloWorld extends Model
{
    public function HelloWorld()
    {
        $sql = "SELECT * FROM " . DB_PREFIX . "category_description"; 
        $implode = array();
        $query = $this->db->query($sql);
        return $query->rows;    
    }
}
?>

管理员\视图\模板\自定义\helloworld.php

<?php echo $header; ?><?php echo $column_left; ?>
<div id='content'>
<h1><?php echo $heading_title; ?></h1>
<?phpecho 'I can also create a custom admin page.!'<br/>; ?>
<?php print_r($my_results);?>
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<li><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a></li>
<?php } ?>
</div> 
<?php echo $footer; ?>
于 2014-10-24T08:21:01.590 回答