BeoWulf
BeoWulf

Reputation: 657

Unable to load controllers from another controller

I'm trying to load a controller inside another controller.

$data['com_top_menu'] = $this->load->controller('account/com_top_menu');

However, this seems to not work when I'm trying to load a controller that is located in the same folder as the controller I'm loading it from.

Tried loading controllers from other folders and seem to not load as well. It seams to load only from the 'common' controllers folder.

Edit:

Actually it seems that the controller is loading. If I place an echo in the middle of the loaded controller it will show the output before the template rendered. So, it looks like the controller is loaded and just doesn't output anything through the rendered view, unless it is a controller inside the common folder. Files are all in place, controller loads, it just doesn't output anything through the view.

Upvotes: 0

Views: 1307

Answers (1)

Nikhil Malik
Nikhil Malik

Reputation: 478

Few things for to load controllers-
1st - you can only load controller from same folders (admin/ catalog).
2nd - you can load controller from any subfolder, just need to pass correct loading path.
3rd - If Opencart hasn't that file than it will not display any error, result will be null/ false.
4th - If you are defining any function name then it will call that function else will call index function so in your case index.

5th - Please use this

 return  $this->load->view('your.tpl', $data);

Instead of

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

6th - Please enable your debug mode from php/ admin so that you will know any error if your code is throwing. Clear your error.log and then try to load controller.

7th - If these all points are code is not working then do 1 thing - add a blank controller with index function and just add one line so that you can return it's result from view then just

echo 'here';

In your view. If OC is not returning this result it's mean you have error in Opencart files else there is error in your code.

You can say these are same in a way (i am not saying completely and don't want to hurt anyone feelings ;)) but this code

$this->load->controller('account/com_top_menu');

is equal to (based on your autoloader)

$obj = new ComTopMenu; //assuming your class name
$data['com_top_menu'] = $obj->index();

so for your solution please check
- you have file com_top_menu.php in your catalog > controller > account >
- your file class name must be ControllerAccountComTopMenu (or any uppercase or lowercase combination but without _)
- your class must have index function because in your case it calling index.

Upvotes: 1

Related Questions