Page 1 of 1

[2.3] Get settings on ajax response

Posted: Thu Oct 05, 2017 8:27 pm
by revington
Hi, I'm writing a new module for opencart 2.3.
This module adds a script to the page, then the script makes an ajax request back to the same controller but different function. Something like:

Code: Select all

class ControllerExtensionModuleTest1 extends Controller {
public function index($setting){ 
//loads a view containing some javascript which will make a POST request back to this controller

public function products(){
}
My first problem is that url 'http://localhost:8000/index.php?route=e ... 1/products' will not hit products($setting). I do need to remove $setting as an argument.
Second problem is that I do not know how to retrieve those settings in a different way.

Also, I've been trying to pass those settings on the url but $this->url->link is converting all "&" into "&" so $this->request->get['param'] does not work.

What I'm doing wrong?

Re: [2.3] Get settings on ajax response

Posted: Thu Oct 05, 2017 10:44 pm
by yodapt
revington wrote:
Thu Oct 05, 2017 8:27 pm
This module adds a script to the page, then the script makes an ajax request back to the same controller but different function.
This makes no sense.

Anyway, if you want to pass parameters to that controller, you can do it using POST or GET methods. If the & gets translated within the request you can still get the data you passed by replacing & with &.

Re: [2.3] Get settings on ajax response

Posted: Fri Oct 06, 2017 12:28 am
by revington
Hi yodapt,

Thanks for your answer. Why does not make sense? Can you be more elaborate? I'm trying to learn about opencart API and best practices and I find it complex because I can not find documentation so I spend a lot of time reading opencart source code.

So, back to my question. You can call invoke any public function from your controller via http call but I don't understand how do I access my module settings (ones specified at the admin part of the module) also, the controller/index function is always called with $settings but any other function does not.

Is there any way to load those settings?
Also, what is the propper way to invoke $this->url->link function? When this function builds the url transaltes & into & later, when I use that url the controller can not access those params via $this->request->get['something'] because the & thing.
I supose there is a good reason for translating & into & but I can not find it.

Thanks,

Pedro

Re: [2.3] Get settings on ajax response

Posted: Fri Oct 06, 2017 1:13 am
by straightlight
Followed is a free developed example that already demonstrates how to work with Opencart API by using AJAX request and response with JSON: viewtopic.php?f=24&t=191768

Re: [2.3] Get settings on ajax response

Posted: Fri Oct 06, 2017 1:41 am
by revington
Hi Straightlight,

Thanks for your reply but my problem is different. I want to know how to load module settings from the controller when the controller function is different from index(). Ie POST .... http://localhost:8000/index.php?route=e ... height=100.
The signature of myfunction is public function myfunction() without the $setting variable. Adding $setting to the signature results in a 404. So, if I can not get the settings that way how do I get them?

Re: [2.3] Get settings on ajax response

Posted: Fri Oct 06, 2017 3:34 am
by yodapt
The settings of a module are available through $this->config->get('desired_key');

Re: [2.3] Get settings on ajax response

Posted: Fri Oct 06, 2017 3:52 am
by straightlight
You can call another controller of any type by using for example:

Code: Select all

$my_module = $this->load->controller('module/my_module');
Assuming the module you're attempting to load would be disabled from the admin and only accessible through your current controller you'd be calling the parent module, you could still acquire that secondary module to your first module. ;)