In Synfony 3.3, the new best practice for DI
is to use normal constructor dependency injection (or "action"
injection in controllers) instead of fetching public services via
$this->get() (though that does still work)
as seen in offical documentation
So no need to specify services as we can type hint them in class controllers :
class InvoiceMailer
{
private $generator;
public function __construct(InvoiceGenerator $generator)
{
$this->generator = $generator
}
// ...
}
This seems to work well, but what if I extends a class and add more parameters in my constructors ???
use Symfony\Component\HttpKernel\Exception\HttpException;
class MyClass extends HttpException
{
private $generator;
public function __construct(InvoiceGenerator $generator, \Exception $previous = null, array $headers = [], $code = 0)
{
$this->generator = $generator;
$statusCode = $generator->getStatusCode();
$message = $generator->getTitle();
parent::__construct($statusCode, $message, $previous, $headers, $code);
}
// ...
}
Now I get a circular reference error :
[Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException]
Circular reference detected for service "AppBundle\Service\MyClass", path: "AppBundle\Service\MyClass -> AppBundle\Service\MyClass".
So, what is the best practice in this case ??
Thanks.
For that case you might have to define the service explicitly as \Exception $previous argument in fact is parent class of MyClass (through HttpException) so the autowiring method try to inject/create an instance of MyClass again on this argument, result: "Circular Reference".
This is an abstraction of what happens to you:
namespace App\Foo;
class MyClass extends \Exception
{
public function __construct(\Exception $previous = null)
{
}
}
Same error, so you can to solve it passing a null value to this argument:
# service.yml
services:
# ...
App\Foo\MyClass:
$previous: ~
or changing its definition manually in a compiler pass or DI extension.
Related
From my controllers, I access the application parameters (those in /app/config) with
$this->container->getParameter('my_param')
But I don't know how to access it from a service (I imagine my service class is not supposed to extend Symfony\Bundle\FrameworkBundle\Controller\Controller).
Should I map needed parameters into my service registration like this:
#src/Me/MyBundle/Service/my_service/service.yml
parameters:
my_param1: %my_param1%
my_param2: %my_param2%
my_param3: %my_param3%
or something similar? How should I access to my application parameters from a service?
This question seems like the same but mine actually answers to it (parameters from a controller), I'm talking about accessing from a service.
You can pass parameters to your service in the same way as you inject other services, by specifying them in your service definition. For example, in YAML:
services:
my_service:
class: My\Bundle\Service\MyService
arguments: [%my_param1%, %my_param2%]
where the %my_param1% etc corresponds to a parameter named my_param1. Then your service class constructor could then be:
public function __construct($myParam1, $myParam2)
{
// ...
}
The Clean Way 2018
Since 2018 and Symfony 3.4 there is much cleaner way - easy to setup and use.
Instead of using container and service/parameter locator anti-pattern, you can pass parameters to class via it's constructor. Don't worry, it's not time-demanding work, but rather setup once & forget approach.
How to set it up in 2 steps?
1. config.yml
# config.yml
parameters:
api_pass: 'secret_password'
api_user: 'my_name'
services:
_defaults:
autowire: true
bind:
$apiPass: '%api_pass%'
$apiUser: '%api_user%'
App\:
resource: ..
2. Any Controller
<?php declare(strict_types=1);
final class ApiController extends SymfonyController
{
/**
* #var string
*/
private $apiPass;
/**
* #var string
*/
private $apiUser;
public function __construct(string $apiPass, string $apiUser)
{
$this->apiPass = $apiPass;
$this->apiUser = $apiUser;
}
public function registerAction(): void
{
var_dump($this->apiPass); // "secret_password"
var_dump($this->apiUser); // "my_name"
}
}
Instant Upgrade Ready!
In case you use older approach, you can automate it with Rector.
Read More
This is called constructor injection over services locator approach.
To read more about this, check my post How to Get Parameter in Symfony Controller the Clean Way.
(It's tested and I keep it updated for new Symfony major version (5, 6...)).
There is a very clean new way to achieve it since symfony 4.1
<?php
// src/Service/MessageGeneratorService.php
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class MessageGeneratorService
{
private $params;
public function __construct(ParameterBagInterface $params)
{
$this->params = $params;
}
public function someMethod()
{
$parameterValue = $this->params->get('parameter_name');
...
}
}
source : https://symfony.com/blog/new-in-symfony-4-1-getting-container-parameters-as-a-service.
Instead of mapping your needed parameters one by one, why not allowing your service to access the container directly? Doing so, you do not have to update your mapping if there is new parameters added (which relate to your service).
To do so:
Make following changes to your service class
use Symfony\Component\DependencyInjection\ContainerInterface; // <- Add this
class MyServiceClass
{
private $container; // <- Add this
public function __construct(ContainerInterface $container) // <- Add this
{
$this->container = $container;
}
public function doSomething()
{
$this->container->getParameter('param_name_1'); // <- Access your param
}
}
Add #service_container as "arguments" in your services.yml
services:
my_service_id:
class: ...\MyServiceClass
arguments: ["#service_container"] // <- Add this
With Symfony 4.1 the solution is quite simple.
Here is a snippet from the original post:
// src/Service/MessageGenerator.php
// ...
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class MessageGenerator
{
private $params;
public function __construct(ParameterBagInterface $params)
{
$this->params = $params;
}
public function someMethod()
{
$parameterValue = $this->params->get('parameter_name');
// ...
}
}
Link to the original post:
https://symfony.com/blog/new-in-symfony-4-1-getting-container-parameters-as-a-service
As solution to some of issues mentioned, I define an array parameter then inject it. Adding a new parameter later just requires addition to parameter array without any change to service_container arguments or construct.
So extending on #richsage answer:
parameters.yml
parameters:
array_param_name:
param_name_1: "value"
param_name_2: "value"
services.yml
services:
my_service:
class: My\Bundle\Service\MyService
arguments: [%array_param_name%]
Then access inside class
public function __construct($params)
{
$this->param1 = array_key_exists('param_name_1',$params)
? $params['param_name_1'] : null;
// ...
}
#richsage is correct (for Symfony 3.?) but it did not work for my Symfony 4.x. So here is for Symfony 4.
in services.yaml file
parameters:
param1: 'hello'
Services:
App\Service\routineCheck:
arguments:
$toBechecked: '%param1%' # argument must match in class constructor
in your service class routineCheck.php file do constructor like so
private $toBechecked;
public function __construct($toBechecked)
{
$this->toBechecked = $toBechecked;
}
public function echoSomething()
{
echo $this->toBechecked;
}
Done.
Symfony 3.4 here.
After some researches, I don't think passing parameters to a class/service via it's constructor, is always a good idea.
Imagine if you need to pass to a controller/service some more parameters than 2 or 3. What then? Would be ridiculous to pass, let's say, up to 10 parameters.
Instead, use the ParameterBag class as a dependency, when declaring the service in yml, and then use as many parameters as you wish.
A concrete example, let's say you have a mailer service, like PHPMailer, and you want to have the PHPMailer connection parameters in the paramters.yml file:
#parameters.yml
parameters:
mail_admin: abc#abc.abc
mail_host: mail.abc.com
mail_username: noreply#abc.com
mail_password: pass
mail_from: contact#abc.com
mail_from_name: contact#abc.com
mail_smtp_secure: 'ssl'
mail_port: 465
#services.yml
services:
app.php_mailer:
class: AppBundle\Services\PHPMailerService
arguments: ['#assetic.parameter_bag'] #here one could have other services to be injected
public: true
# AppBundle\Services\PHPMailerService.php
...
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
...
class PHPMailerService
{
private $parameterBag;
private $mailAdmin;
private $mailHost;
private $mailUsername;
private $mailPassword;
private $mailFrom;
private $mailFromName;
private $mailSMTPSecure;
private $mailPort;
}
public function __construct(ParameterBag $parameterBag)
{
$this->parameterBag = $parameterBag;
$this->mailAdmin = $this->parameterBag->get('mail_admin');
$this->mailHost = $this->parameterBag->get('mail_host');
$this->mailUsername = $this->parameterBag->get('mail_username');
$this->mailPassword = $this->parameterBag->get('mail_password');
$this->mailFrom = $this->parameterBag->get('mail_from');
$this->mailFromName = $this->parameterBag->get('mail_from_name');
$this->mailSMTPSecure = $this->parameterBag->get('mail_smtp_secure');
$this->mailPort = $this->parameterBag->get('mail_port');
}
public function sendEmail()
{
//...
}
I think this is a better way.
In symfony 4, we can access the parameters by means of dependency injection:
Services:
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
MyServices {
protected $container;
protected $path;
public function __construct(Container $container)
{
$this->container = $container;
$this->path = $this->container->getParameter('upload_directory');
}
}
parameters.yml:
parameters:
upload_directory: '%kernel.project_dir%/public/uploads'
I'm trying to test my Category class. I'm using Mockery::mock() method, with 'overload:' prefix and makePartial() method.
When running test I have this error:
Mockery\Exception\BadMethodCallException : Method App\Models\Category::getDynamicFieldsForDocument() does not exist on this mock object
Here is my code:
namespace App\Models;
class Category extends DictionaryBase{
//some methods
public function getDynamicFieldsForDocument()
{
$data = [];
$parents = [];
$allParents = $this->getParents($this->id, $parents);
foreach ($allParents as $parentId) {
$parent = Category::find($parentId);
$fields = $parent->dynamicFields();
foreach ($fields as $field) {
$data[$field['name']] = $field;
}
}
return $data;
}
}
TestCase:
namespace Tests\Unit;
use App\Models\Category;
use Tests\TestCase;
class CategoryModelTest extends TestCase{
//some methods
/**
* #runInSeparateProcess
* #preserveGlobalState disabled
*/
public function testGetDynamicFieldsForDocument()
{
$mockCategory = \Mockery::mock('overload:'.Category::class)->makePartial();
$preparedDynamicFields = $this->prepareDynamicFields();
$preparedCategories = $this->prepareCategories();
$mockCategory->shouldReceive('find')->andReturn($preparedCategories[0], $preparedCategories[1], $preparedCategories[2]);
$mockCategory->shouldReceive('getParents')->andReturn(['1a2b', '3c4d', '5e6f']);
$mockCategory->shouldReceive('dynamicFields')->andReturn(null, $preparedDynamicFields[0], $preparedDynamicFields[1]);
$response = $mockCategory->getDynamicFieldsForDocument();
dd($response);
}
}
I have no idea why i still have error. I think when ->makePartial() method is called it should mock only methods, which are called by ->shouldReceive()
EDIT:
Now I'm making mock instance without :overload, and mocking 'find' method in this way:
`$mockCategory->shouldReceive('find')->andReturn($preparedCategories[0], $preparedCategories[1], $preparedCategories[2]);`
My find method looks like this:
public static function find($id) {
return $id ? self::list(config(static::IDENT.'.fields'), (new Filter('and'))->add('id', $id, '')->toArray(),[],1,1)[0] ?? null : null;
}
And this is my error:
Error : Wrong parameters for App\Exceptions\ApiException([string
$message [, long $code [, Throwable $previous = NULL]]])
It's because list method call API so it looks like this method is called without mock.
I know that i can't mock static method, but earlier when I used :overload it was possible. What's now?
Delete :overload and just define your mock as:
$mockCategory = \Mockery::mock(Category::class)->makePartial()
Example
Model:
namespace App\Models;
class Foobar extends BaseModel
{
public function foonction()
{
Foobar::find();
return '1';
}
}
Test:
namespace Tests;
use Evalua\Heva\Models\Foobar;
class FoobarTest extends TestCase
{
public function testFoobar()
{
$fooMock = \Mockery::mock('overload:'.Foobar::class)->makePartial();
$fooMock->shouldReceive('find')->once();
$fooMock->foonction();
}
}
Fails with:
Mockery\Exception\BadMethodCallException: Method Evalua\Heva\Models\Foobar::foonction() does not exist on this mock object
Without the :overload the test pass.
The explanation should be based on what's written in the documentation about overload:
Prefixing the valid name of a class (which is NOT currently loaded) with “overload:” will generate an alias mock (as with “alias:”) except that created new instances of that class will import any expectations set on the origin mock ($mock). The origin mock is never verified since it’s used an expectation store for new instances. For this purpose we use the term “instance mock”
I have a Sumfony 4.3 command that processes some data and loops through a number of "processors" to do the processing. The code uses a factory (autowired) which then instantiates the command.
use App\Entity\ImportedFile;
use App\Service\Processor\Processor;
class Factory implements FactoryInterface
{
/** #var array */
private $processors;
/** #var TestClausesInterface */
private $testClauses;
private $em;
private $dataSetProvider;
private $ndviFromNasaService;
private $archivalHashService;
private $mailer;
private $projectDir;
public function __construct(
TestClausesInterface $testClauses,
ValidProcessorList $processors,
EntityManagerInterface $em,
DataSetProvider $dataSetProvider,
NDVIFromNasaService $ndviFromNasaService,
ArchivalHashService $archivalHashService,
\Swift_Mailer $mailer,
$projectDir)
{
$this->processors = $processors;
$this->testClauses = $testClauses;
$this->em = $em;
$this->dataSetProvider = $dataSetProvider;
$this->ndviFromNasaService = $ndviFromNasaService;
$this->archivalHashService = $archivalHashService;
$this->mailer = $mailer;
$this->projectDir = $projectDir;
}
public function findProcessorForFile(ImportedFile $file)
{
...
if ($found){
$candidates = $this->recursive_scan( $this->projectDir.'/src/Processor');
foreach ($candidates as $candidate){
if (substr($candidate,0,strlen('Helper')) === 'Helper'){
continue;
}
try {
$candidate = str_replace($this->projectDir.'/src/Processor/', '', $candidate);
$candidate = str_replace('/','\\', $candidate);
$testClassName = '\\App\\Processor\\'.substr( $candidate, 0, -4 );
/* #var Processor $test */
if (!strstr($candidate, 'Helper')) {
$test = new $testClassName($this->testClauses, $this->em, $this->dataSetProvider, $this->ndviFromNasaService, $this->archivalHashService, $this->mailer, $this->projectDir);
}
However I still have to:
autowire all arguments both in the Factory and Processor top class
pass all arguments in correct order to the Processor
I have around 70 subclasses of Processor. All of them use EntityInterface, but only a couple use SwiftMailer and the other dependencies.
As I am adding services to be used only by a few Processors, I am looking for a way to autowire these arguments only at the Processor level. Ideally, also without adding service definitions to services.yml
In summary, I would like to be able to add a dependency to any subclass of Processor, even if it is a parent class of other subclasses and have the dependency automatically injected.
There is much it is not immediately obvious in your code, but the typical way to resolve this is by using a "service locator". Docs.
Let's imagine you have several services implementing the interface Processor:
The interface:
interface Processor {
public function process($file): void;
}
Couple implementation:
class Foo implements Processor
{
public function __construct(DataSetProvider $dataSet, ArchivalHashService $archivalHash, \Swift_Mailer $swift) {
// initialize properties
}
public function process($file) {
// process implementation
}
public static function getDefaultIndexName(): string
{
return 'candidateFileOne';
}
}
Couple implementations:
class Bar implements Processor
{
public function __construct(\Swift_Mailer $swift, EntityManagerInterface $em) {
// initialize properties
}
public function process($file) {
// process implementation
}
public static function getDefaultIndexName(): string
{
return 'candidateFileTwo';
}
}
Note that each of the processors have completely different dependencies, and can be auto-wired directly, and that each of them has a getDefaultIndexName() method.
Now we'll "tag" all services implementing the Processor interface:
# services.yaml
services:
# somewhere below the _defaults and the part where you make all classes in `src` available as services
_instanceof:
App\Processor:
tags:
- { name: "processor_services", default_index_method: 'getDefaultIndexName' }
Attention here: The documentation says that if you define a public static function getDefaultIndexName() it will be picked by default. But I've found this not to be working at the moment. But if you define the default_index_method you can wire it to a method of your choice. I'm keeping the getDefaultIndexName for the time being, but you can pick something of your own choice.
Now, if you need this processes in a console command, for example:
use Symfony\Component\DependencyInjection\ServiceLocator;
class MyConsoleCommand
{
private ServiceLocator $locator;
public function __construct(ServiceLocator $locator)
{
$this->locator = $locator;
}
}
To inject the service locator you would do:
#services.yaml
services:
App\HandlerCollection:
arguments: [!tagged_locator { tag: 'processor_services' } ]
And to fetch any of the processors from the service locator you would do:
$fooProcessor = $this->locator->get('candidateFileOne');
$barProcessor = $this->locator->get('candidateFileTwo');
Summping up, basically what you need is:
Define a shared interface for the processors
Use that interface to tag all the processor services
Define a getDefaultIndexName() for each processor, which helps you match files to processors.
Inject a tagged service locator in the class that need to consume this services
And you can leave all services auto-wired.
Note: You could use an abstract class instead of an interface, and it would work the same way. I prefer using an interface, but that's up to you.
For completion sake, here is a repo with the above working for Symfony 4.3.
I mean something that acts like unittest.mock.patch in Python.
Is it possible in PHP?
-- UPDATE1 --
I'm using PHPUnit and try to mock the dependencies of SUT that is LoginController.
One of dependencies is the Fatfree Base. It's used directly in LoginController's constructor as Base::instance().
So, what I need is it to substitute the original Base class globally with the my mock class object. To make SUT use mocked Base::instance(). The only way I found is to provide this mock object on constructor of LoginController. To make this I was obligated to change the orignial SUT code that doesn't consider unit test requirements.
But I don't like this, due to there are many places in SUT code where different dependencies are used. So I need to mock those in PHP "class registry" globally to force "client" code use my class definitions to create objects.
In couple of words I need to mock class definition on system-wide level.
I'm looking for several hours on the net and still can't find the solution.
class LoginControllerTest extends TestCase
{
public function setUp()
{
$MockF3Base = $this->createMock(Base::class);
$MockF3Base->method('get')->will($this->returnCallback(
function($p) {
$mock_params = [
'LANGUAGES'=>['en', 'ru'],
'COOKIE.lg'=>'en',
'user'=>new GuestModel(),
'AJAX'=>true,
'BASE'=>BASE_URL
];
if (array_key_exists($p, $mock_params))
{
return $mock_params[$p];
}
return null;
}));
$sut = $this->getMockBuilder(LoginController::class)
->setConstructorArgs([$MockF3Base])
->setMethods(['json_success'])
->getMock();
$this->_sut = $sut;
}
public function testMustLoginIf__()
{
$this->_sut->expects($this->once())->method('json_success')->with($this->stringContains(BASE_URL.'/kassa'));
$this->_sut->LoginAction();
}
private $_sut;
}
-- UPDATE2 --
Optional $f3 constructor parameter is added by me. Originally it was just $this->f3 = Base::instance();
class LoginController{
public function __construct($f3 = null){
$this->f3 = is_null($f3)? Base::instance(): $f3;
$this->section = $this->f3->get('PARAMS.section') or $this->section = 'index';
$this->name = substr(get_class($this), 0, -10);
$user = new GuestModel();
$session = $this->f3->get('COOKIE.PHPSESSID');
if($session && $user->load(['`session`=?', $session])){
}
$this->f3->set('user', $user);
$language = $this->f3->get('COOKIE.lg');
$language = array_key_exists($language, $this->f3->get('LANGUAGES')) ? $language : 'en';
$this->f3->set('LANGUAGE', $language);
if(!$user->dry() && ($user->session('language') != $language)){
$user->session('language', $language, true);
}
}
}
From my controllers, I access the application parameters (those in /app/config) with
$this->container->getParameter('my_param')
But I don't know how to access it from a service (I imagine my service class is not supposed to extend Symfony\Bundle\FrameworkBundle\Controller\Controller).
Should I map needed parameters into my service registration like this:
#src/Me/MyBundle/Service/my_service/service.yml
parameters:
my_param1: %my_param1%
my_param2: %my_param2%
my_param3: %my_param3%
or something similar? How should I access to my application parameters from a service?
This question seems like the same but mine actually answers to it (parameters from a controller), I'm talking about accessing from a service.
You can pass parameters to your service in the same way as you inject other services, by specifying them in your service definition. For example, in YAML:
services:
my_service:
class: My\Bundle\Service\MyService
arguments: [%my_param1%, %my_param2%]
where the %my_param1% etc corresponds to a parameter named my_param1. Then your service class constructor could then be:
public function __construct($myParam1, $myParam2)
{
// ...
}
The Clean Way 2018
Since 2018 and Symfony 3.4 there is much cleaner way - easy to setup and use.
Instead of using container and service/parameter locator anti-pattern, you can pass parameters to class via it's constructor. Don't worry, it's not time-demanding work, but rather setup once & forget approach.
How to set it up in 2 steps?
1. config.yml
# config.yml
parameters:
api_pass: 'secret_password'
api_user: 'my_name'
services:
_defaults:
autowire: true
bind:
$apiPass: '%api_pass%'
$apiUser: '%api_user%'
App\:
resource: ..
2. Any Controller
<?php declare(strict_types=1);
final class ApiController extends SymfonyController
{
/**
* #var string
*/
private $apiPass;
/**
* #var string
*/
private $apiUser;
public function __construct(string $apiPass, string $apiUser)
{
$this->apiPass = $apiPass;
$this->apiUser = $apiUser;
}
public function registerAction(): void
{
var_dump($this->apiPass); // "secret_password"
var_dump($this->apiUser); // "my_name"
}
}
Instant Upgrade Ready!
In case you use older approach, you can automate it with Rector.
Read More
This is called constructor injection over services locator approach.
To read more about this, check my post How to Get Parameter in Symfony Controller the Clean Way.
(It's tested and I keep it updated for new Symfony major version (5, 6...)).
There is a very clean new way to achieve it since symfony 4.1
<?php
// src/Service/MessageGeneratorService.php
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class MessageGeneratorService
{
private $params;
public function __construct(ParameterBagInterface $params)
{
$this->params = $params;
}
public function someMethod()
{
$parameterValue = $this->params->get('parameter_name');
...
}
}
source : https://symfony.com/blog/new-in-symfony-4-1-getting-container-parameters-as-a-service.
Instead of mapping your needed parameters one by one, why not allowing your service to access the container directly? Doing so, you do not have to update your mapping if there is new parameters added (which relate to your service).
To do so:
Make following changes to your service class
use Symfony\Component\DependencyInjection\ContainerInterface; // <- Add this
class MyServiceClass
{
private $container; // <- Add this
public function __construct(ContainerInterface $container) // <- Add this
{
$this->container = $container;
}
public function doSomething()
{
$this->container->getParameter('param_name_1'); // <- Access your param
}
}
Add #service_container as "arguments" in your services.yml
services:
my_service_id:
class: ...\MyServiceClass
arguments: ["#service_container"] // <- Add this
With Symfony 4.1 the solution is quite simple.
Here is a snippet from the original post:
// src/Service/MessageGenerator.php
// ...
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class MessageGenerator
{
private $params;
public function __construct(ParameterBagInterface $params)
{
$this->params = $params;
}
public function someMethod()
{
$parameterValue = $this->params->get('parameter_name');
// ...
}
}
Link to the original post:
https://symfony.com/blog/new-in-symfony-4-1-getting-container-parameters-as-a-service
As solution to some of issues mentioned, I define an array parameter then inject it. Adding a new parameter later just requires addition to parameter array without any change to service_container arguments or construct.
So extending on #richsage answer:
parameters.yml
parameters:
array_param_name:
param_name_1: "value"
param_name_2: "value"
services.yml
services:
my_service:
class: My\Bundle\Service\MyService
arguments: [%array_param_name%]
Then access inside class
public function __construct($params)
{
$this->param1 = array_key_exists('param_name_1',$params)
? $params['param_name_1'] : null;
// ...
}
#richsage is correct (for Symfony 3.?) but it did not work for my Symfony 4.x. So here is for Symfony 4.
in services.yaml file
parameters:
param1: 'hello'
Services:
App\Service\routineCheck:
arguments:
$toBechecked: '%param1%' # argument must match in class constructor
in your service class routineCheck.php file do constructor like so
private $toBechecked;
public function __construct($toBechecked)
{
$this->toBechecked = $toBechecked;
}
public function echoSomething()
{
echo $this->toBechecked;
}
Done.
Symfony 3.4 here.
After some researches, I don't think passing parameters to a class/service via it's constructor, is always a good idea.
Imagine if you need to pass to a controller/service some more parameters than 2 or 3. What then? Would be ridiculous to pass, let's say, up to 10 parameters.
Instead, use the ParameterBag class as a dependency, when declaring the service in yml, and then use as many parameters as you wish.
A concrete example, let's say you have a mailer service, like PHPMailer, and you want to have the PHPMailer connection parameters in the paramters.yml file:
#parameters.yml
parameters:
mail_admin: abc#abc.abc
mail_host: mail.abc.com
mail_username: noreply#abc.com
mail_password: pass
mail_from: contact#abc.com
mail_from_name: contact#abc.com
mail_smtp_secure: 'ssl'
mail_port: 465
#services.yml
services:
app.php_mailer:
class: AppBundle\Services\PHPMailerService
arguments: ['#assetic.parameter_bag'] #here one could have other services to be injected
public: true
# AppBundle\Services\PHPMailerService.php
...
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
...
class PHPMailerService
{
private $parameterBag;
private $mailAdmin;
private $mailHost;
private $mailUsername;
private $mailPassword;
private $mailFrom;
private $mailFromName;
private $mailSMTPSecure;
private $mailPort;
}
public function __construct(ParameterBag $parameterBag)
{
$this->parameterBag = $parameterBag;
$this->mailAdmin = $this->parameterBag->get('mail_admin');
$this->mailHost = $this->parameterBag->get('mail_host');
$this->mailUsername = $this->parameterBag->get('mail_username');
$this->mailPassword = $this->parameterBag->get('mail_password');
$this->mailFrom = $this->parameterBag->get('mail_from');
$this->mailFromName = $this->parameterBag->get('mail_from_name');
$this->mailSMTPSecure = $this->parameterBag->get('mail_smtp_secure');
$this->mailPort = $this->parameterBag->get('mail_port');
}
public function sendEmail()
{
//...
}
I think this is a better way.
In symfony 4, we can access the parameters by means of dependency injection:
Services:
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
MyServices {
protected $container;
protected $path;
public function __construct(Container $container)
{
$this->container = $container;
$this->path = $this->container->getParameter('upload_directory');
}
}
parameters.yml:
parameters:
upload_directory: '%kernel.project_dir%/public/uploads'