How to inject a service in another service in Symfony?
You pass service id as argument to constructor or setter of a service.
Assuming your other service is the userbundle_service
:
userbundle_service:
class: Main\UserBundle\Controller\UserBundleService
arguments: [@security.context, @logger]
Now Logger is passed to UserBundleService
constructor provided you properly update it, e.G.
protected $securityContext;
protected $logger;
public function __construct(SecurityContextInterface $securityContext, Logger $logger)
{
$this->securityContext = $securityContext;
$this->logger = $logger;
}
Dependency injection from service to another service not working symfony 5
You are not retrieving the NewUser
class from the container, but instancing it manually, so Dependency Resolution is not happening and the service is not reciving any of its dependencies. You should inject the service into the controller for dependency resolution to occur, or pass the arguments explicitly when instancing it.
public function register(Request $request,
UserPasswordEncoderInterface $passwordEncoder,
GuardAuthenticatorHandler $guardHandler,
UserAuthenticator $authenticator,
NewUser $newUser): Response
{
//...
$newUserRequest = new NewUserRequest();
//...
// $newUser = new NewUser(); // Not passing The Database or PasswordEncoder dep
$newUser->execute($newUserRequest);
//...
}
Inject one service inside another service
- check Typos. The file ServiceOne.php should contain one class ServiceOne and should be named in service.yml as ServiceOne (with namespace)
you should not define a service twice, AppBundle\Service\ServiceOne: ~ will fit
you should active autowiring. this means you don't need to configure thoses services. Only if you need public usage. But than you still dont need to confige the arguments
Look here Autowiring : https://symfony.com/doc/current/service_container/autowiring.html
and here autoloading: https://symfony.com/doc/3.4/service_container.html#injecting-services-config-into-a-service
Symfony PHP - Using service within another service
In short you need to use dependency injection.
You are correct that by registering the Database
class arguments in services.yml
, Symfony is able to inject these values during instantion. However, this is only done automatically when using the dependency injection container.
You will need to use this in both your LoginController
and in your Session
service.
LoginController
namespace App\Controller
use App\Services\Session;
class LoginController
{
private $session;
public function __construct(Session $session)
{
$this->session = $session;
}
}
Session
namespace App\Services;
use App\Services\Database;
class Session
{
private $db;
public function __construct(Database $db)
{
$this->db = $db;
}
public function create(){
$this->db->insert('a new session'); // (pseudo-code)
}
}
how to use a service inside another service in symfony 2.6
This call is fetching service from DI container, which you dont have in your service
$this->get('manage_ge_proc');
It works in controller because DI container is automatically injected there.
Since you have this line in you services.yml
, which tells Symfony to inject @manage_de_proc
service into ge_lib
constructor
arguments: [@session, @doctrine.orm.entity_manager, @manage_ge_proc]
you should be able to pick @manage_ge_proc
from constructor like this:
public function __construct(
Session $session,
EntityManager $entityManager,
GEManageProcedure $manageGeProc
)
{
//... whatever you do in your constructor
$this->manageGeProc = $manageGeProc;
}
Is it possible to inject a method into a service in symfony services.yml
Credit to qooplmao for their comment in helping me find the answer as this is exactly what I was looking for. I thought I'd answer my own question for the benefit of others as I now have this working, plus some corrections to the syntax in the comment.
What I should have been looking for was Symfony's Expression Language which allows precisely the granularity of control I was looking for.
The resulting configuration now looks like this:
object_cache:
class: App\Bundle\ApiBundle\Service\ObjectCache
some_service:
class: App\Bundle\ApiBundle\Service\SomeService
arguments: [@=service('object_cache').getUser()]
For more information on the Expression Syntax, here is some detailed documentation: http://symfony.com/doc/2.7/components/expression_language/syntax.html
(If only Symfony docs had the courtesy to provide links to such crucial information on pages that make reference to it!)
Related Topics
How to Change the Name of an Element in Dom
Where to Use MySQL_Real_Escape_String to Prevent SQL Injection
Count Word Frequency in a Text
Is It Acceptable to Use a Mix of Object Oriented Style with Procedural Style in Coding PHP
Http Header for Downloading Microsoft Word and Excel Files
How to Send a File via Axios to Laravel
How Check Memory Location of Variable in PHP
Namevaluepair Error "Namevaluepair Cannot Be Resolved to a Type"
Redirect to New Page W/ Post Data (Php/Zend)
Why Are My PHP Tags Converted to HTML Comments
How to Validate Non-English (Utf-8) Encoded Email Address in JavaScript and PHP