Post by b&p » Tue Oct 09, 2012 12:07 pm

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

User avatar
b&p
New member

Posts

Joined
Wed Jun 08, 2011 10:18 pm

Post by Avvici » Wed Oct 10, 2012 11:17 am

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.

User avatar
Expert Member

Posts

Joined
Tue Apr 05, 2011 12:09 pm
Location - Asheville, NC

Post by b&p » Wed Oct 10, 2012 9:12 pm

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.

User avatar
b&p
New member

Posts

Joined
Wed Jun 08, 2011 10:18 pm

Post by Avvici » Wed Oct 10, 2012 9:39 pm

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.

User avatar
Expert Member

Posts

Joined
Tue Apr 05, 2011 12:09 pm
Location - Asheville, NC

Post by b&p » Wed Oct 10, 2012 11:16 pm

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.

User avatar
b&p
New member

Posts

Joined
Wed Jun 08, 2011 10:18 pm

Post by Avvici » Wed Oct 10, 2012 11:46 pm

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

User avatar
Expert Member

Posts

Joined
Tue Apr 05, 2011 12:09 pm
Location - Asheville, NC

Post by b&p » Wed Oct 10, 2012 11:56 pm

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

User avatar
b&p
New member

Posts

Joined
Wed Jun 08, 2011 10:18 pm

Post by JAY6390 » Wed Nov 28, 2012 8:00 pm


Image


User avatar
Guru Member

Posts

Joined
Wed May 26, 2010 11:47 pm
Location - United Kingdom
Who is online

Users browsing this forum: Semrush [Bot] and 88 guests