speed-providing-uncl Posted February 16, 2018 Share Posted February 16, 2018 (edited) Hi, I would like to know how I can access `global $kernel` on a front module? The dump says it's null I used to do `global $kernel` in modules/mymodule/controllers/admin/ and it's working fine!! But it's not working in /override/controllers/front/IndexController.php for example Any idea? We are migrating from symfony to prestashop Kind regards Florian Edited February 16, 2018 by Florian (see edit history) Link to comment Share on other sites More sharing options...
hhennes Posted February 17, 2018 Share Posted February 17, 2018 Hello, In fact symfony is only implemented in a few pages in the adminstration. ( Product and modules pages for ps 1.7.2.4 ) So as far as i know there is no way to use the symfony variables in the front office. Regards, Link to comment Share on other sites More sharing options...
speed-providing-uncl Posted February 17, 2018 Author Share Posted February 17, 2018 (edited) Yeah this is what I thought... I just found a way to create it from inside a Front Controller : require_once _PS_ROOT_DIR_.'/app/AppKernel.php'; class IndexController extends IndexControllerCore{ public function initContent(){ global $kernel; dump($kernel); // null $kernel = new \AppKernel('prod', false); dump($kernel); // kernel with null container $kernel->boot(); dump($kernel->getContainer()); // Container OK, we can call $kernel->getContainer()->get('service') die(); } } Thanks Florian Source: full article in https://blog.floriancourgey.com/2018/05/how-to-work-with-the-symfony-kernel-anywhere-in-prestashop-1-7/ PS : refacto to put on in /override/classes/controller/Controller : protected $kernel; public function __construct(){ global $kernel; dump($kernel); // can return null if(!$kernel){ // if null, we boot it manually require_once _PS_ROOT_DIR_.'/app/AppKernel.php'; $kernel = new \AppKernel('prod', false); dump($kernel); // kernel without container $kernel->boot(); // kernel with container } dump($kernel->getContainer()); // yay ! $this->kernel = $kernel; } Edited September 16, 2020 by Florian fix link + php syntax (see edit history) Link to comment Share on other sites More sharing options...
Estian Posted September 16, 2020 Share Posted September 16, 2020 A module I'm busy modifying/fixing is having issues with its Ajax request in the PS BO. It imports product data - but it is not updating the "Catalog -> Stocks -> Movements" list. From what I can tell this is because the line "$sfContainer = SymfonyContainer::getInstance();" within the "StockManager" class is returning "NULL". I am assuming here that the $kernel object is not being initialized properly because the AJAX request is being made via a custom .php file (root of the module). require_once('../../config/config.inc.php'); require_once('../../init.php'); The above code in the AJAX custom file does not appear to solve the issue. I have also tried "booting" $kernel but to no avail: global $kernel; if(!$kernel){ require_once _PS_ROOT_DIR_.'/app/AppKernel.php'; $kernel = new \AppKernel('prod', false); $kernel->boot(); } How does one go about "booting up" $kernel? Would it possibly fix the issue if the Ajax request went through a Module Admin Ajax Controller instead of a custom file? Link to comment Share on other sites More sharing options...
masterblaster Posted April 9, 2021 Share Posted April 9, 2021 On 9/16/2020 at 4:37 PM, Estian said: From what I can tell this is because the line "$sfContainer = SymfonyContainer::getInstance();" within the "StockManager" class is returning "NULL". I am assuming here that the $kernel object is not being initialized properly because the AJAX request is being made via a custom .php file (root of the module). Hi @Estian , did you managed to fix it in some way ? I'm struggling with the same problem... SymfonyContainer::getInstance(); is returning null in StockManager class when called by PS webservice to update order status, preventing stock movement from being registered. Looks like a PS bug, but I can't figure out how to properly boot the container. Tried already with boostrapping the Kernel or loading the container from context using the adapter but always getting Fatal error: Uncaught Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "prestash op.core.api.stock_movement.repository". Link to comment Share on other sites More sharing options...
Ali Samie Posted July 5, 2022 Share Posted July 5, 2022 On 2/18/2018 at 2:35 AM, Florian said: Yeah this is what I thought... I just found a way to create it from inside a Front Controller : require_once _PS_ROOT_DIR_.'/app/AppKernel.php'; class IndexController extends IndexControllerCore{ public function initContent(){ global $kernel; dump($kernel); // null $kernel = new \AppKernel('prod', false); dump($kernel); // kernel with null container $kernel->boot(); dump($kernel->getContainer()); // Container OK, we can call $kernel->getContainer()->get('service') die(); } } Thanks Florian Source: full article in https://blog.floriancourgey.com/2018/05/how-to-work-with-the-symfony-kernel-anywhere-in-prestashop-1-7/ PS : refacto to put on in /override/classes/controller/Controller : protected $kernel; public function __construct(){ global $kernel; dump($kernel); // can return null if(!$kernel){ // if null, we boot it manually require_once _PS_ROOT_DIR_.'/app/AppKernel.php'; $kernel = new \AppKernel('prod', false); dump($kernel); // kernel without container $kernel->boot(); // kernel with container } dump($kernel->getContainer()); // yay ! $this->kernel = $kernel; } This is interesting. But when you load the other container for admin controllers, how to you fix this error? "Argument 1 passed to PrestaShop\PrestaShop\Adapter\Employee\ContextEmployeeProvider::__construct() must be an instance of Employee, null given, called in /var/www/html/***********/var/cache/prod/Container4xg96ta/getPrestashop_Adapter_DataProvider_EmployeeService.php on line 8" Link to comment Share on other sites More sharing options...
Mehdib92 Posted September 29, 2022 Share Posted September 29, 2022 (edited) Hi, I had the same problem and the article (https://blog.floriancourgey.com/2018/05/how-to-work-with-the-symfony-kernel-anywhere-in-prestashop-1-7/) worked for me. Thanks Edited September 30, 2022 by Mehdib92 add link (see edit history) Link to comment Share on other sites More sharing options...
Damian Posted April 18, 2023 Share Posted April 18, 2023 When you load a symfony kernel by your self (in front office), sometimes other parts of code required context. You have to load it by your self too. $kernel = new AppKernel(_PS_ENV_, _PS_MODE_DEV_); $kernel->boot(); $kernel ->getContainer() ->get('prestashop.adapter.legacy_context_loader') // it's PrestaShop\PrestaShop\Adapter\LegacyContextLoader ->load<context name to load>Context(<context parameters>) ; Here you can read more https://devdocs.prestashop-project.org/1.7/development/components/console/context-helper/ 1 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