Back to Top

Ajax call on same controller while using front controller

Updated 16 January 2017

Ajax call on same controller while using front controller to perform some custom action using jquery ajax in prestashop.

We have seen many times in admin controller when they call ajax request to perform some action, they refer the same admin controller in ajax url instead of different controller, so that they don’t need to create any other controller to get response and perform any custom action on that.

Below is the example code where they have used same admin controller to get the response from the call which triggered to get or perform some action.

Example of admin controller –

Jquery Ajax Call

$.ajax({
	url : yourAdminController.php
	type : 'POST',
	cache : false,
	data : {
		ajax : true,
		variable_1 : var1,
		variable_2 : var2,
		action : doSomeAction,
	},
	success : function (result) {
		// your action code on result
	}
});

Admin Controller Code

<?php

/**
* 2010-2016 Webkul.
*
* NOTICE OF LICENSE
*
* All right is reserved,
* Please go through this link for complete license : https://store.webkul.com/license.html
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this module to newer
* versions in the future. If you wish to customize this module for your
* needs please refer to https://store.webkul.com/customisation-guidelines/ for more information.
*
*  @author    Webkul IN <[email protected]>
*  @copyright 2010-2016 Webkul IN
*  @license   https://store.webkul.com/license.html
*/

class YourAdminControllerController extends ModuleAdminController
{
    public function __construct()
    {
        $this->identifier = 'id';
        parent::__construct();
        $this->bootstrap = true;
        // your own code
    }

    // your own functions
    // your own functions
    // your own functions
    // your own functions


    This function will get ajax call where pass two variables and a method name doSomeAction    
    public function ajaxProcessDoSomeAction()
    {
    	var1 = Tools::getValue('variable_1');
    	var2 = Tools::getValue('variable_2');

    	// your action code ....
    }
    
}

In Prestashop, we can follow this processor same in our front controller as well.

Searching for an experienced
Prestashop Company ?
Find out More

Here, only different is displayAjax and ajaxProcess.

  • In Admin Controller – we need to prefix “ajaxProcesss” with our action function which we have passed in our ajax call , while getting ajax call.
  • In front controller – we need to prefix “displayAjax” with our action function which we have passed in our ajax call, while getting ajax call.

See the example code for front controller

jQuery Ajax Call

$.ajax({
	url : yourFrontController.php
	type : 'POST',
	cache : false,
	data : {
		ajax : true,
		variable_1 : var1,
		variable_2 : var2,
		action : doSomeAction,
	},
	success : function (result) {
		// your action code on result
	}
});

Front Controller Code –

<?php

/**
* 2010-2016 Webkul.
*
* NOTICE OF LICENSE
*
* All right is reserved,
* Please go through this link for complete license : https://store.webkul.com/license.html
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this module to newer
* versions in the future. If you wish to customize this module for your
* needs please refer to https://store.webkul.com/customisation-guidelines/ for more information.
*
*  @author    Webkul IN <[email protected]>
*  @copyright 2010-2016 Webkul IN
*  @license   https://store.webkul.com/license.html
*/

class ModuleNameYourFrontControllerFrontController extends ModuleFrontController
{
    public function initContent()
    {
    	parent::initContent();
    	// your own code
    }

    // your own functions
    // your own functions
    // your own functions
    // your own functions


    This function will get ajax call where pass two variables and a method name doSomeAction    
    public function displayAjaxDoSomeAction()
    {
    	var1 = Tools::getValue('variable_1');
    	var2 = Tools::getValue('variable_2');

    	// your action code ....
    }
    
}

In this way, we can get ajax call on our same controller either on admin side or front side. It will save your extra controller code as well as make your code more readable for each developer.

If there are something not understandable by this post then do comment below. We will surely assist on that.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


2 comments

  • 杨志
    • Webkul Support
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home