从Prestashop模块发出ajax请求

从Prestashop模块发出ajax请求,prestashop,prestashop-1.7,Prestashop,Prestashop 1.7,我正在制作一个模块,我需要制作一个ajax请求,如果可能的话,还需要JSON响应,我该怎么做? 我不太了解Prestashop 1.7在这方面的结构 谢谢 这非常简单,只需使用Prestashop的标准制作控制器,然后将其链接到前端Javascript即可 将一个php文件命名如下:./modules/modulename/controllers/front/ajax.php 然后放入: <?php // Edit name and class according to your fil

我正在制作一个模块,我需要制作一个ajax请求,如果可能的话,还需要JSON响应,我该怎么做? 我不太了解Prestashop 1.7在这方面的结构


谢谢

这非常简单,只需使用Prestashop的标准制作控制器,然后将其链接到前端Javascript即可

将一个php文件命名如下:./modules/modulename/controllers/front/ajax.php

然后放入:

<?php

// Edit name and class according to your files, keep camelcase for class name.
require_once _PS_MODULE_DIR_.'modulename/modulename.php';

class ModuleNameAjaxModuleFrontController extends ModuleFrontController
{
    public function initContent()
    {

        $module = new ModuleName;

        // You may should do some security work here, like checking an hash from your module
        if (Tools::isSubmit('action')) {

            // Usefull vars derivated from getContext
            $context = Context::getContext();
            $cart = $context->cart;
            $cookie = $context->cookie;
            $customer = $context->customer;
            $id_lang = $cookie->id_lang;

            // Default response with translation from the module
            $response = array('status' => false, "message" => $module->l('Nothing here.'));

            switch (Tools::getValue('action')) {

                case 'action_name':

                    // Edit default response and do some work here
                    $response = array('status' => true, "message" => $module->l('It works !'));
                    
                    break;

                default:
                    break;

            }
        }

        // Classic json response
        $json = Tools::jsonEncode($response);
        echo $json;
        die;

        // For displaying like any other use this method to assign and display your template placed in modules/modulename/views/template/front/...
        // Just put some vars in your template
        // $this->context->smarty->assign(array('var1'=>'value1'));
        // $this->setTemplate('template.tpl');

        // For sending a template in ajax use this method
        // $this->context->smarty->fetch('template.tpl');

    }
}

?>
在前端,您只需在JS文件中这样调用它(这里使用jQuery):


瞧,您已经准备好使用ajax控制器了吗?真的吗?使用Symfony\Component\HttpFoundation\JsonResponse;这是很久以前的事了,最好使用本机解决方案,但这个解决方案会拒绝相同的答案。使用fondamental Symfony不是强制性的,因此不向后兼容…是的,我在那之后用谷歌搜索了一下,旧PS真的没有任何“最佳实践”解决方案。。。死亡真是一团糟。。。
// In your module PHP
public function hookFooter($params)
{
    
    // Create a link with the good path
    $link = new Link;
    $parameters = array("action" => "action_name");
    $ajax_link = $link->getModuleLink('modulename','controller', $parameters);

    Media::addJsDef(array(
        "ajax_link" => $ajax_link
    ));

}
// ajax_link has been set in hookfooter, this is the best way to do it
$(document).ready(function(){

    $.getJSON(ajax_link, {parameter1 : "value"}, function(data) {

        if(typeof data.status !== "undefined") {

            // Use your new datas here
            console.log(data);

        }

    });

});