Jump to content

Recommended Posts

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 by Florian (see edit history)
Link to comment
Share on other sites

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 by Florian
fix link + php syntax (see edit history)
Link to comment
Share on other sites

  • 2 years later...

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

  • 6 months later...
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

  • 1 year later...
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

  • 2 months later...
  • 6 months later...

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/

  • Like 1
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...