aymeric69001 Posted August 16, 2023 Share Posted August 16, 2023 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 More sharing options...
endriu107 Posted August 16, 2023 Share Posted August 16, 2023 11 minutes ago, aymeric69001 said: return $this->render('@Modules/mysupermodule/views/templates/admin/app.tpl'); Try change this to: return $this->context->smarty->fetch($this->local_path.'views/templates/admin/app.tpl'); Link to comment Share on other sites More sharing options...
aymeric69001 Posted August 16, 2023 Author Share Posted August 16, 2023 (edited) thanks but if you look at the error it comes from this->module->context->smarty->assign It basically says that this->module->context->smarty is null Edited August 16, 2023 by aymeric69001 (see edit history) Link to comment Share on other sites More sharing options...
endriu107 Posted August 16, 2023 Share Posted August 16, 2023 There shouldn't be "module" just $this->context->smarty->assign Link to comment Share on other sites More sharing options...
aymeric69001 Posted August 16, 2023 Author Share Posted August 16, 2023 (edited) I know, I tested it without "module" as well, same error... Actually, I remember for PS1.7 and 1.6 controllers were in ...controller directory of the module, but now there is this src dir, things get more complicated and do not work ! Edited August 16, 2023 by aymeric69001 (see edit history) Link to comment Share on other sites More sharing options...
endriu107 Posted August 16, 2023 Share Posted August 16, 2023 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 More sharing options...
aymeric69001 Posted August 16, 2023 Author Share Posted August 16, 2023 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 More sharing options...
endriu107 Posted August 16, 2023 Share Posted August 16, 2023 Add context Context::getContext() Link to comment Share on other sites More sharing options...
aymeric69001 Posted August 16, 2023 Author Share Posted August 16, 2023 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 More sharing options...
AddWeb Solution Posted August 17, 2023 Share Posted August 17, 2023 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 More sharing options...
aymeric69001 Posted August 17, 2023 Author Share Posted August 17, 2023 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 More sharing options...
aymeric69001 Posted August 17, 2023 Author Share Posted August 17, 2023 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now