Jump to content

How to use Smarty in a module admin controller ?


Recommended Posts

Hello,

I am trying to build a simple admin controller from my module, so in modules/mysupermodule/src/Controller I created this file: DemoController.php

 

<?php
namespace MySuperModule\Controller;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
class DemoController extends FrameworkBundleAdminController
{
    public function demoAction()
    {
        $this->module->context->smarty->assign([
            'pathApp' => $this->module->getPathUri() . 'views/js/app.js',
            'chunkVendor' => $this->module->getPathUri() . 'views/js/chunk-vendors.js',
        ]);
        return $this->render('@Modules/mysupermodule/views/templates/admin/app.tpl');
    }
}

 

and in modules/mysupermodule/config/routes.yml:

 

your_route_name:
    path: mysupermodule/demo
    methods: [GET]
    defaults:
      _controller: 'MySuperModule\Controller\DemoController::demoAction'

 

Finally, I also created a composer.json with this content:

 

{
    "name": "aymeric/mysupermodule",
    "description": "...",
    "autoload": {
      "psr-4": {
        "MySuperModule\\Controller\\": "src/Controller/"
      }
    },
    "config": {
        "prepend-autoloader": false
    },
    "type": "prestashop-module"
}

and did composer dumpautoload from the module dir

 

but then when I access http://localhost/prestashop/admin322w21vmubv6v/modules/mysupermodule/demo

 

I get

Quote

Call to a member function assign() on null

 

Any idea ?

 

Thanks

 

 

Link to comment
Share on other sites

You still have issue with passing data to smarty or with access to view tpl file?

I think in controller in initContent you should add this and if previous answer is not working you can try with:

$this->setTemplate('module:mysupermodule/views/templates/admin/app.tpl');

Link to comment
Share on other sites

My problem is not about accessing the tpl file. It's only about passing data to smarty.

The problem is that $this->context is not returning anything, look at this updated code:

 

namespace MySuperModule\Controller;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;

class DemoController extends FrameworkBundleAdminController
{
    public function demoAction()
    {
        $this->context; /// JUST FOR SHOWING THE ERROR 
        $this->context->smarty->assign([
            'pathApp' => $this->module->getPathUri() . 'views/js/app.js',
            'chunkVendor' => $this->module->getPathUri() . 'views/js/chunk-vendors.js',
        ]);
        return $this->render('@Modules/mysupermodule/views/templates/admin/app.tpl');
    }    
}

 

it produces this error

 

Notice: Undefined property: MySuperModule\Controller\DemoController::$context

 

 

Link to comment
Share on other sites

1 minute ago, endriu107 said:

Add context Context::getContext()

I tried this already, it returns:

 

Attempted to load class "Context" from namespace "MySuperModule\Controller".
Did you forget a "use" statement for e.g. "HTMLPurifier_Context" or "PrestaShop\PrestaShop\Adapter\Shop\Context"?

Link to comment
Share on other sites

14 hours ago, aymeric69001 said:

I tried this already, it returns:

 

Attempted to load class "Context" from namespace "MySuperModule\Controller".
Did you forget a "use" statement for e.g. "HTMLPurifier_Context" or "PrestaShop\PrestaShop\Adapter\Shop\Context"?

It seems like the error message indicates that you're trying to use a class named Context within your module's namespace,but it should actually be PrestaShop\PrestaShop\Adapter\Shop\Context. Make sure to use the correct namespace when accessing the context.

Code should be something like,

<?php
namespace MySuperModule\Controller;

use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use PrestaShop\PrestaShop\Adapter\Shop\Context;

class DemoController extends FrameworkBundleAdminController
{
    public function demoAction()
    {
        $context = Context::getContext();

        $this->module->context->smarty->assign([
            'pathApp' => $this->module->getPathUri() . 'views/js/app.js',
            'chunkVendor' => $this->module->getPathUri() . 'views/js/chunk-vendors.js',
        ]);
        return $this->render('@Modules/mysupermodule/views/templates/admin/app.tpl');
    }
}

Clear the cache and try if it works

Thanks!

Link to comment
Share on other sites

I tried of course... here is what I got

Attempted to call an undefined method named "getContext" of class "PrestaShop\PrestaShop\Adapter\Shop\Context".
Did you mean to call e.g. "getContextListShopID", "getContextListShopIDUsingCustomerSharingSettings", "getContextShopGroup" or "getContextShopID"?

Regards

Link to comment
Share on other sites

Solution:

 

<?php

namespace MySuperModule\Controller;

use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use Symfony\Component\HttpFoundation\Response;

use Context;
use ModuleCore;

class DemoController extends FrameworkBundleAdminController
{
    
    public function demoAction()
    {
        $context = Context::getContext();
        $module = ModuleCore::getInstanceByName("mysupermodule");        
        $context->smarty->assign([
            'pathApp' => $module->getPathUri() . 'views/js/app.js',
            'chunkVendor' => $module->getPathUri() . 'views/js/chunk-vendors.js',
        ]);
        die($context->smarty->fetch('module:'.'mysupermodule'.'/views/templates/admin/app.tpl')); 
    }
}

 

Conclusion: we must call the context via the getContext static function (which is not the case for a frontController, a comment from prestashop core developer would be welcome !), the function of an adminController must return a Response Object and just returning $context->smarty->.... produces an error, I found this trick with the die function...

 

 

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...