前端控制器不会从setMedia在prestashop 1.7模块上加载css和js
问题描述:
我正在为prestashop 1.7.2.1编写prestashop模块。前端控制器不会从setMedia在prestashop 1.7模块上加载css和js
我创建了一个前端控制器为我的模块,下面的代码:
<?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和我注意到,这些代码行真正得到执行,但是当我尝试浏览我的前端控制器具有以下网址:
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错误(我也有DEV启用prestashop)。
答
如果资产路径错误,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');
谢谢。我从现在开始使用这个..但它仍然提供相同的结果。与之前使用的路径相同,文件不加载 – ufk
我会尝试调试以查看其失败的位置 – ufk
@ufk您可以调试它[此处](https://github.com/PrestaShop/PrestaShop/blob/1.7 .2.x /班/资产/ JavascriptManager.php#L57)。在标记行之前添加if($ id =='module-tuxinmodcartype-cartypeproducts-js')die($ this-> getFullPath($ relativePath));'这应该会告诉您AssetManager是否正确解析链接。 – TheDrot