Prestashop 1.7.7.5:FrameworkBundleAdminController:$this->module->getPathUri() 返回错误

问题描述

在我的管理模块的控制器的 listMappingsAction 操作中,我想显示一个 Vue.JS 模板,传递两个需要调用 $this->module->getPathUri() 来赋值的变量:

<?PHP
namespace XYZ\Controller;

use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use PrestaShopBundle\Security\Annotation\AdminSecurity;


class AutomatizedMappingController extends FrameworkBundleAdminController
{

     /**
     * @AdminSecurity("is_granted('read',request.get('_legacy_controller'))",message="Access denied.")
     */
    function listMappingsAction() {
        return  $this->render('@Modules/XYZ/views/templates/admin/app.html.twig',[
            'XYZ' => [
                'pathApp' => $this->module->getPathUri() . 'views/js/app.js','chunkvendor' => $this->module->getPathUri() . 'views/js/chunk-vendors.js',]
        ]);
    }
}

使用 $this->module->getPathUri() 会导致显示错误

尝试调用类“XYZ\Controller\AutomatizedMappingController”的名为“getPathUri”的未定义方法。 [Symfony\Component\Debug\Exception\UndefinedMethodException 0]

我可以调用什么来使它工作?文档没有提到这个用例... https://devdocs.prestashop.com/1.7/modules/concepts/templating/vuejs/faq/#how-to-send-data-from-symfony-controller-to-vuejs

解决方法

首先,您需要知道模块和管理控制器之间存在差异。您不能调用 $this->module->getPathUri(),因为您没有使用 extends ModuleAdminControllerextends ModuleFrontControllerextends Module ...

因此,您只能在实际使用模块内的文件时调用 $this->module

因为我不知道您要走哪条路,所以向您传递了一些您可能可以使用的变量。

$this->module->name              = crezzurmodulename
$this->module->getLocalPath()    = C:\yourstore/modules/crezzurmodulename/
$this->module->getPathUri()      = /yourstore/modules/crezzurmodulename/
_MODULE_DIR_                     = /yourstore/modules/
_PS_MODULE_DIR_                  = C:\yourstore/modules/
__DIR__                          = C:\yourstore\modules\crezzurmodulename\controllers\front
_PS_CAT_IMG_DIR_                 = C:\yourstore/img/c/
_PS_PROD_IMG_DIR_                = C:\yourstore/img/p/
_PS_TMP_IMG_DIR_                 = C:\yourstore/img/tmp/
_PS_ROOT_DIR_                    = C:\yourstore
_PS_CACHE_DIR_                   = C:\yourstore/var/cache/dev\
_PS_BASE_URL_                    = http://127.0.0.1
__PS_BASE_URI__                  = /yourstore/
_PS_TRANSLATIONS_DIR_            = C:\yourstore/translations/
_PS_BASE_URL_SSL_                = http://127.0.0.1 or https://127.0.0.1
_PS_DOWNLOAD_DIR_                = C:\yourstore/download/
_PS_COL_IMG_DIR_                 = C:\yourstore/img/co/
_PS_SHIP_IMG_DIR_                = C:\yourstore/img/s/
_PS_UPLOAD_DIR_                  = C:\yourstore/upload/