1

I want to use the parameters in parameters.yml in my service class mailer but I got this error while instantiate the mailer class:

$mailer = new Mailer();

knowing that the parameters are defined in parameters.yml:

Warning: Missing argument 1 for AppBundle\Service\Mailer::__construct(), called in

namespace AppBundle\Service


class Mailer
{
private $mailer_user;
private $mailer_password;
private $mailer_name;
private $mailer_host;
public function __construct($mailer_user, $mailer_password ,$mailer_name ,$mailer_host)
{
    $this->mailer_name = $mailer_user;
    $this->mailer_password = $mailer_password;
    $this->mailer_user = $mailer_name;
    $this->mailer_host = $mailer_host;
}
//.....
}

services.yml:

mailer:
    class:    'AppBundle\Service\Mailer'
    arguments:    [%mailer_user%, %mailer_password% ,%mailer_name% ,%mailer_host%]
6
  • 1
    You should specify what error you get (some exception or what?) because "doesnt work" is not enought. PS: you should remove the dollar sign from the parameter value and from the service argument %$mailer_host% (is this a typo?). Commented Aug 15, 2017 at 16:28
  • @gp_sflover is it the right way to do it ? error =>Whoops, looks like something went wrong. Commented Aug 15, 2017 at 16:32
  • 1
    Why are you repeating the mailer parameters in services.yml? Or is that just some kind of copy/paste thing? Commented Aug 15, 2017 at 17:13
  • @Cerad thanks again for your help i removed the repaeated parametrs but i got a new error if you can help me Commented Aug 16, 2017 at 8:47
  • @Achraf you should not change the question when it has been resolved (with a novice it could be a never-ending question :-)) but open a new one for each NEW issue. Commented Aug 16, 2017 at 9:31

1 Answer 1

1

To learn "how can I use the parameters in parameters.yml in my service class" (and to see how a service can be used in a Symfony app) just read the Symfony docs paying attention to the Symfony version of the documentation you are reading:

BTW you should never instanciate a service class directly like you did:

$mailer = new Mailer();

but retrieve the service instance from the Service Container (Symfony will take care to automatically inject all the configured dependancies) like we usually do in a Controller (the example below access the Service using the shortcode provided extending the base Controller provided by the FrameworkBundle included in the Symfony standard version):

$mailer = $this->container->get('mailer');
Sign up to request clarification or add additional context in comments.

1 Comment

This was solid advice while it lasted, however using '$this->container->get('mailer')' is deprecated now and is dropped in symfony 4. They want you to use DI in stead.

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.