| Install | |
|---|---|
composer require prestashop/module-lib-service-container |
|
| Latest Version: | v2.0 |
| PHP: | >=5.6.0 |
This repository includes the service container from Symfony that you can use in your PrestaShop Module.
You should install this library only on a PrestaShop environment and with PHP 5.6.0 minimum.
# PrestaShop 1.7+
composer require prestashop/module-lib-service-container
# PrestaShop 1.6
composer require prestashop/module-lib-service-container
composer require symfony/config:^3.4 symfony/dependency-injection:^3.4 symfony/expression-language:^3.4 symfony/yaml:^3.4
When this project is successfully added to your dependencies, you can add the new ServiceContainer to your module and use it. PrestaShop runs with Symfony components from version 1.7, so dependancies are not required anymore here. I you plan to run your module on PrestaShop, Symfony dependencies must be required separately.
To use this library, it's simple :
/**
* @var ServiceContainer
*/
private $serviceContainer;
$this->serviceContainer = new ServiceContainer($this->name, $this->getLocalPath());
/**
* @param string $serviceName
*
* @return mixed
*/
public function getService($serviceName)
{
return $this->serviceContainer->getService($serviceName);
}
getService();We split the services in two folders in the config : /front and /admin folders. So the tree should be like :
/mymodule
/config
/front
services.yml
/admin
services.yml
common.yml
imports:
- { resource: ../common.yml }
Now you can add your services in the services.yml like you were in a Symfony project ;)