前端控制器不会在prestashop 1.7模块上从setMedia加载css和js



我正在为 prestashop 1.7.2.1 编写一个 prestashop 模块。

我使用以下代码为我的模块创建了一个前端控制器:

<?php
require_once (__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'php'.
DIRECTORY_SEPARATOR.'TuxInDb.php');
class TuxInModCarTypeCarTypeProductsModuleFrontController extends ModuleFrontController {
private $tuxDb;
public function initContent(){
parent::initContent();
$productIds = [];
$this->tuxDb = TuxInDb::getInstance();
$companyName = Tools::getValue('company_name');
$modelName = Tools::getValue('model_name');
$year = Tools::getValue('year');
$month = Tools::getValue('month');
$carType = Tools::getValue('car_type');
$carListCarTypeIds=$this->tuxDb->getCarListCarTypeIds($companyName,$modelName,$carType,$year,$month);
$productIds = $this->tuxDb->getProductIdsByCarListCarTypeIds($carListCarTypeIds);
$this->context->smarty->assign('product_ids',$productIds);
$this->setTemplate('module:tuxinmodcartype/views/templates/front/cartypeproducts.tpl');
}
public function setMedia() {
parent::setMedia();
$this->registerStylesheet('module-tuxinmodcartype-cartypeproducts-style','modules/'.$this->module->name.'/css/cartypeproducts.css');
$this->registerJavascript('module-tuxinmodcartype-cartypeproducts-js','modules/'.$this->module->name.'/js/cartypeproducts.js');
}
}

正如您在setMedia()函数中看到的那样,我加载了 css 和 js 文件。 我什至在 xdebug 中调试了它,我注意到这些代码行实际上被执行了,但是当我尝试使用以下 url 浏览我的前端控制器时:

http://prestashop.dev:8080/index.php?company_name=BMW&model_name=SERIA+1&year=2011&month=1&car_type=5+%D7%93%D7%9C%D7%AA%D7%95%D7%AA+%28%D7%94%D7%90%D7%A6%D7%B3%D7%91%D7%A7%29&fc=module&module=tuxinmodcartype&controller=cartypeproducts&id_lang=1

我检查了我的谷歌浏览器的网络选项卡,我注意到我需要的 js 和 css 文件没有加载。

有什么想法吗?

我没有看到javascript错误或php错误(我也在prestashop中启用了DEV(。

如果资产路径错误,那么Prestashop甚至不会将其附加到浏览器的<head>(或底部,具体取决于CCC设置(,并且不会抛出任何错误。

可能您的路径不正确,要获得正确的路径,请使用以下命令:

$this->registerStylesheet('module-tuxinmodcartype-cartypeproducts-style', $this->module->getPathUri() . 'css/cartypeproducts.css');
$this->registerJavascript('module-tuxinmodcartype-cartypeproducts-js', $this->module->getPathUri() . 'js/cartypeproducts.js');

适用于PrestaShop 1.7.x

在 ModuleFrontController 中添加以下内容:

public function setMedia()
{
parent::setMedia();
$this->addCSS($this->module->getPathUri().'views/css/style.css');
}

我希望这有帮助!

最新更新