5

I am havin a problem adding an admin page to Opencart2, and following the answers on pretty much identical questions on SO do no help, so I beleive the problem is specific to OC2.

Following the answer at this question I still get the error message "Fatal error: Call to undefined method ControllerCustomHelloWorld::render() in C:\websites\weddingshoponline\shop\admin\controller\custom\helloworld.php on line 13. Any help would be much appreciated, as I have been going around in circles.

Thank you.

PS Reverting to a previous version of OC is not valid response, albeit a good one.

Community
  • 1
  • 1
Gavin Simpson
  • 2,766
  • 3
  • 30
  • 39
  • 1
    In OC2 you are no longer calling `$this->render()`. The problem is you followed tutorial/answer applicable for OC < 2.0. Take a look in other controllers in OC 2.0 to find out how the page rendering is done (basically there is none, you only set the variables for rendering - template, contents of module positions, done). – shadyyx Oct 24 '14 at 06:40
  • Yup, I did that thanks for replying. It did bring up a new problem with the $header and $footer variables being undeclared, but I was hoping not to have to slog this one out as I still have a way to go with creating a new module in oc2. – Gavin Simpson Oct 24 '14 at 06:43
  • I didn't want to write it here, but why not - SO is missing good questions and good answers as well... Check my answer. – shadyyx Oct 24 '14 at 08:01
  • Yes, why not? Your answer is correct, and I have completed adding a simple admin page. Your answer is pretty much correct, so I will accept it as such and post the working files as a sample in a mo'. I was just being doff on the $headers/$footers comment. – Gavin Simpson Oct 24 '14 at 08:16

2 Answers2

19

The difference between page rendering in OC < 2.0 and OC 2.0 are only few but you have to be aware of them.

1. $data

In OC < 2.0 you would do this:

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

while in OC 2.0 it is only $data, i.e.

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

that is passed over to the $this->load->view() method as an argument, e.g.:

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

2. $this->render()

is gone. Now you are calling $this->load->view('catalog/category_list.tpl', $data) instead.

3. $this->children

is gone. Now the template child modules' positions are instantiated as part of template properties while you have to call their controllers manually (WHY?):

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

I was thinking why the hell were these changes required. What has been improved? Did they want the developers to write less code? Is it now more following the OOP, MVC, WTF (sorry) principles? And got the answer: NO (or nothing to the first one).

We still have to load the translations (I mean, we still have to load each single string translation). And gettext is out there for more than 8 years...

Instead of short $this->response->setOutput($this->render()); we now have to call much longer (and incomprehensible) $this->response->setOutput($this->load->view('catalog/category_form.tpl', $data));. Why can't we just do this: $this->render('catalog/category_form.tpl', $data); ???

I personally think OC 2.0 is the same excrement (from the developers perspective) as it was before. They just changed the packaging. But, honestly, there are even bigger excrements out there, that's why I'm stuck with OpenCart :-)

shadyyx
  • 15,825
  • 6
  • 60
  • 95
5

Elaborating on shadyyx's anwser to the question, herewith the code I got working... I am not saying it's perfect, just it works.

admin\controller\custom\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!';
    ?>

admin\model\custom\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;    
    }
}
?>

admin\view\template\custom\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; ?>
Gavin Simpson
  • 2,766
  • 3
  • 30
  • 39