Narayana Pusarla
Narayana Pusarla

Reputation: 41

How to call a controller from a view in opencart?

I'm new to opencart. I want to create a custom theme and some custom controllers and models. I can't find any tutorials relative to this, but I tried to create a view along a controller. When I call that view from home or header view page, like $header (in home file) and $search (in header file), then it shows undefined variable.

My code looks like this. It's in controller (path is catalog\controller\common\test.php).

<?php
class ControllerCommonTest extends Controller{
    public function index() {
 if(file_exists(DIR_TEMPLATE.this->config->get('config_template').'/template/test/test.tpl')) {

        $this->response->setOutput($this->render());
    } else {
        return $this->load->view('default/template/common/header.tpl');
    }
    }
}
?>

And my view is in \view\theme\MyTheme\template\common\test.tpl

<?php
echo "Test file";
?>

And in my home file, I call my controller like below...

<?php
echo $header;
echo $test;
echo $footer;
?>

When I run this it shows the below error:

Notice: Undefined variable: test in C:\xampp\htdocs\opencart\catalog\view\theme\MyCustome\template\common\home.tpl on line 4

So, please provide any tutorial links and any examples for developing a custom module in opencart.

Thanks in advance.

Upvotes: 2

Views: 4495

Answers (1)

zed Blackbeard
zed Blackbeard

Reputation: 760

To display test module tpl i.e. test.tpl on home page, You have load test controller on home controller. Please add following code in catalog/controller/common/home.php

add this code

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

After

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

Upvotes: 2

Related Questions