Search code examples
prestashopprestashop-1.7

Prestashop 1.7.7.5: FrameworkBundleAdminController: $this->module->getPathUri() returns an error


In my admin module's controller's listMappingsAction action, I want to show a Vue.JS template, passing two variables that need to call $this->module->getPathUri() to be assigned a value:

<?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',
            ]
        ]);
    }
}

The use of $this->module->getPathUri() results in this error being displayed:

Attempted to call an undefined method named "getPathUri" of class "XYZ\Controller\AutomatizedMappingController". [Symfony\Component\Debug\Exception\UndefinedMethodException 0]

What could I call to make it work? The docs don't mention this use case... https://devdocs.prestashop.com/1.7/modules/concepts/templating/vuejs/faq/#how-to-send-data-from-symfony-controller-to-vuejs


Solution

  • First of all you need to know there is a difference between a module and a admin-controller. You cannot call $this->module->getPathUri() because your not using a extends ModuleAdminController, extends ModuleFrontController, extends Module ...

    So you can only call $this->module if your actually using files inside a module.

    Since i don't know which path you are trying to go to ill pass you a few variables which you might be able to use.

    $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/