Page 1 of 1

Opencart 1.5+ calling custom function from module controller

Posted: Tue Oct 09, 2012 12:07 pm
by b&p
Hi guys, I've been wondering if I can call custom function from module controller.
I've written some extensions before where I have a custom function called from the controller, for example:
if I edit admin/controller/sale/customer.php and after index() function add

Code: Select all

public function foo(){
//code here
}
I can access it by using

Code: Select all

index.php?route=sale/customer/foo
Now I have a module in catalog, could I access a function from it's controller, in the below example "foo"?
my_module.php:

Code: Select all

class ControllerModuleMyModule extends Controller {
	protected function index($setting) {
...
}
public function foo(){
...
}
Thanks in advance for any insights.
B&P

Re: Opencart 1.5+ calling custom function from module contro

Posted: Wed Oct 10, 2012 11:17 am
by Avvici
It's not really clear what you want but if you want to access a function from within the controller just call it like this:

Code: Select all

$this->foo(); 
You can also set up the call to the function to be echoed in the TPL (from the controller) either by href or by using AJAX.

Re: Opencart 1.5+ calling custom function from module contro

Posted: Wed Oct 10, 2012 9:12 pm
by b&p
Thanks for your reply. Sorry if I wasn't clear.
On pages that have their own url (route) like index.php?route=account/customer, I can access custom function by just appending function name to the url, like this: index.php?route=account/customer/my_function

But for modules, there are no urls or routes as they are loaded on the fly into other pages.
That was my question. I do need to access it from the .tpl, i.e. when I click a button it makes ajax call to this module's controller, if that does make any sense.

Example, I add a function my_fuction() to the account/customer.php

Code: Select all

my_function(){
echo 'foo';
}
Then I can make an ajax call or access it directly through this url:
index.php?route=account/customer/my_function

It will return or output: foo

Now, if I add the same function to a module's controller, how would I access it. Modules are loaded into index.php and we can have them on any page and can have multiple instances of the same module on a single page.

Re: Opencart 1.5+ calling custom function from module contro

Posted: Wed Oct 10, 2012 9:39 pm
by Avvici
Ah yes. I see. There is no difference at all to be honest. A controller is a controller. For example if you take the categories module (which displays categories on a floating box) . In the category.tpl you could put an ajax function that contacts a public function inside the module/category.php controller, just like you are planning on doing. No difference at all.

Re: Opencart 1.5+ calling custom function from module contro

Posted: Wed Oct 10, 2012 11:16 pm
by b&p
Could you demonstrate please. if categories box is on home page, how do I go about it? index.php?route=common/home/my_function ?
Because, obviously index.php?route=modules/category/my_function won't work (I did try it though..)

Thanks.

Re: Opencart 1.5+ calling custom function from module contro

Posted: Wed Oct 10, 2012 11:46 pm
by Avvici
No, you are thinking about this the wrong way. Just because it is on the home page doesn't mean you mess with the home page controller. It's a module, and positioned on different pages depending upon what your settings are. It will look the same on all pages. Just mess with module/category.php and module/category.tpl

Re: Opencart 1.5+ calling custom function from module contro

Posted: Wed Oct 10, 2012 11:56 pm
by b&p
Yes, I am confused.;) thought I am getting hang of this MVC thing until this. Can you give an example of how would I call the controller function from the .tpl? I want to make an AJAX call to the controller from a button in my .tpl

Re: Opencart 1.5+ calling custom function from module contro

Posted: Wed Nov 28, 2012 8:00 pm
by JAY6390