The reason you can get $ this-> container in the controller is because it is injected into the controller that you are expanding.
For example, you can enter into a container and configure it in your constructor.
services.yml
notification: class: %project.notification.class% arguments: [@templating, @doctrine]
NotificationService.php
<?php private $container; public function __construct() { $this->container = $container; } public function mailStuff() { if ( $this->container->get('kernel')->getEnvironment() == "dev" ) { mail( ' mymail@mail.com ', $lib, $txt, $entete ); } else { mail( $to->getEmail(), $lib, $txt, $entete ); } }
See dependency injection for more information.
NOTE
In general, an injection in a container is bad and means that there is a better way to do something. In this case, Symfony already has a solution that we are trying to solve.
Enter SwiftMailer.
In particular, the section on sending all dev messages to a given address .
Try setting up Swiftmailer and add the following to your dev configuration.
application / Config / config _dev.yml
swiftmailer: delivery_address: ' dev@example.com '
Alex l
source share