0

I've created a service MailService. Where is the right place for the service configuration? Is it the file services.yml? If I write

App\Service\MailService:
      someparam: somevalue

I get the error message The configuration key someparam is unsupported for definition App\Service\MailService. How to configure the service properly? How to read the params within my service?

class MailService
{
    public function __construct()
    {
    }

}
2
  • 1
    Please insert MailService source code to your question Commented Jan 29, 2019 at 10:03
  • I have inserted Commented Jan 29, 2019 at 10:25

1 Answer 1

2

You can pass some data by constructor parameters and correct service configuration:

Service class:

class MailService
{
    private $adminEmail;

    private $adminName;

    public function __construct($adminEmail, $adminName)
    {
        $this->adminEmail = $adminEmail;
        $this->adminName = $adminName;
    }

}

Services configuration:

App\Service\MailService:
    arguments:
        $adminEmail: '[email protected]'
        $adminName: 'Admin Name'

Read more about Dependency Injection and autowiring in Symfony 4 projects


enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I've read, I can do so: parameters: adminEmail: someValue App\Service\MailService: arguments: ['%adminEmail%'] For what do I need the section in parameters? What is the advantage?
All about parameters in Symfony you can read in documentation: symfony.com/doc/current/best_practices/… You can use parameters to define configuration options which can change. For example: default language or path to your private JWT key... Look above at the picture I added

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.