Adding Admin Page in OpenCart Version 2

I have a problem adding an admin page to Opencart2, and after answering almost the same questions about SO, do not help, so I believe that the problem is related to OC2.

After answering this question, I still get the error " Fatal error: calling the undefined ControllerCustomHelloWorld :: render () method in C: \ websites \ weddingshoponline \ shop \ admin \ controller \ custom \ helloworld.php on line 13. Any help would be greatly appreciated as I gather in circles.

Thanks.

PS Returning to a previous version of OC is not valid, although good.

+5
source share
2 answers

The difference between page rendering in OC <2.0 and OC 2.0 is just a few, but you should be aware of them.

1. $data

On OC <2.0, you would do this:

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

whereas in OC 2.0 it is just $data , i.e.

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

which is passed to the $this->load->view() method as an argument, for example:

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

2. $this->render()

disappeared. Now you call $this->load->view('catalog/category_list.tpl', $data) .

3. $this->children

disappeared. Now the positions of the template child modules are created as part of the template properties, while you must manually call their controllers (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 thought why the hell are these changes necessary. What has been improved? Did they want developers to write less code? Is it now more consistent in terms of OOP, MVC, WTF (sorry)? And he received the answer: NO (or nothing for the first).

We still have to download translations (I mean , we still have to download every line-feed ). And gettext has been there for over 8 years ...

Instead of short $this->response->setOutput($this->render()); we need 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 that OC 2.0 is the same excrement (from the point of view of developers) as before. They just changed the packaging. But to be honest, there is even more excrement, so I'm stuck in OpenCart :-)

+20
source

While developing a question on shadyyx anwser, I worked with the code ... I do not say that it is perfect, it just works.

admin \ controller \ user \ 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; ?> 
+5
source

Source: https://habr.com/ru/post/1205421/


All Articles