I writing an application with Zend Framework 2, and Doctrine 2. I have 2 month experience with both technologies.
I using Doctrine repository documents scheme. I need to access current user in repository class. I can't figure out how to get serviceManager instance in repository. I read lot of articles, but still can't understand dependency injection, I need to to manage service in ZF2.
Can someone explain me how to inject service to an abstract class (like base repository)?
You need to implement ServiceLocatorAwareInterface to lazy retrieve the serviceManager.
Quote from ZF2 documents:
Additionally, there are some cases where you may want to receive the ServiceManager to lazy-retrieve dependencies; as such, you may want to implement ServiceLocatorAwareInterface and know more details about the API of the ServiceManager.
See ZF2 Quick Start - service manager.
But to be honest you should not use any services within your repository classes. Keep them simple and clean just to retrieve data.
And when you call your repository class, you should pass the user like so:
$data = $this->getEntityManager()->getRepository('Common\Entity\User')->myMethod($user);
Related
Update 2020 (this should have been much sooner). There can be a new class for each controller action. Each controller can be named by the action it is going to do, with an Invoke() method. Consider "Action Domain Responder" (ADR).
Why do I want to do this? Controllers do not necessarily adhere to SRP, and I'm not about to go creating a new class for each of, what is effectively, a controller 'action'. Therefore, a controller should not have everything injected via constructor, but the relevant method being called should be able to state explicitly "I require these objects" and another method "these other objects".
As of Symfony 2.8, the Dependency Injection component now provides auto-wiring for your services if you provide autowire: true in services.yml.
I'm defining my controller as a service, like so:
test_controller:
class: AppBundle\Controller\TestController
autowire: true
My controller looks as follows:
class TestController
{
public function indexAction(TestClass1 $tc1, $id)
{
return new Response('The slug is: ' . $id);
}
}
You will notice I'm typehinting for TestClass, which is just an empty class. However, the following error appears when I refresh the page:
What do I need to change in my services.yml file to have auto wiring dependency injection in my controller method? Just a note, the issue isn't because I have an $id 'slug' afterwards. Removing it does nothing.
Edit: I've created a bundle allowing to do more or less what you want called DunglasActionBundle.
Disclaimer: I'm the author of the Symfony autowiring system.
The Symfony Dependency Injection Component (and the autowiring system is part of it) doesn't work that way. It only allows to automatically inject dependencies in the class constructor of services and knows nothing about controller classes, actions and other parts of the HttpKernel Component.
It's not currently possible to do what you want to do. Initially, the autowiring system has been designed for domain services, not for controllers.
It should be possible to bridge the autowiring system with controller parameters using a custom param converter. However, I'll suggest you take another way I've first described here:
There is another approach I want to discuss since several time but I
did not have the time to blog about it.
It's a derivate of/similar to the ADR pattern, applied to Symfony.
An action is a single class with a __invoke() method containing all it's
logic (should be only some lines of glue code to call the domain and
the responder).
Actions are registered as a service (it can be
automated using a compiler pass and some conventions, similar to how
we discover controllers right now, with the ability to override the
definition if needed)
On such action services, autowiring is enabled.
It means that almost as easy as current controllers to use for the
developper, but with an explicit dependency graph and a better
reusability. Only things that are really necessary are injected. In
fact, it's already doable to use such system with Symfony. We do it in
API Platform master. Here is one action for instance:
https://github.com/dunglas/DunglasApiBundle/blob/master/Action/PostCollectionAction.php
In this implementation I also rely a lof on kernel events to be able
to plug and play some sort of logic (persistence, validation...) in a
decoupled manner but it's out of scope.
I have build a in-house MVC PHP framework and now I am struggling on implementing DI Container. I've adopted the Pimple as a DiC, I have read the book by Chris Hartjes "The Grumpy Programmer's Guide To Building Testable PHP Applications" (which I find a very good and inspiring read, will recommend it highly!), which talks eased me to understand more about TDD. Anyway, If I get a DI in the core of the framework, how should I populate the definitions and ho should I pass it along.
Injecting the container(Injecting in the application object all the way to the user created controller). - WRONG
Forcing the dev-user to 'populate' it in the Bootstrap - WRONG
Singleton - VERY WRONG
Observer Pattern (DiC attached to the observer. Observer as a front end to DiC) - ?(Probably the worst idea :D )
Then how to make the core DiC available in the whole framework (for, lets say, injecting the Configuration Object), without creating any dependencies, unnecessary forcing the user to code it or adding the overhead of creating XML/JSON or any other.
PS:
** I do believe that I will see a lot of answers about the Inversion-of-Control (IoC) and Service Locator. Which I cant seem to get exactly how to implement them.. Reference me to a simple/basic guide.
(disclaimer, I'm the developer of PHP-DI)
I don't really understand your question, it seems to me that if you have full control over the MVC framework, then using DI and a DIC should be pretty easy. Here is a copy of the introduction to DI on PHP-DI homepage:
Application needs FooController so:
Application gets FooController from the Container, so:
Container creates SomeRepository
Container creates BarService and gives it SomeRepository
Container creates FooController and gives it BarService
Application calls FooController
FooController calls BarService
BarService calls SomeDependency
SomeRepository does something
The Container is in charge of creating all the objects (the object graph), and then the non-framework code (controllers, services, …) works without EVER calling the Container.
Then how to make the core DiC available in the whole framework?
Do not make it available in the whole framework.
Each component (object) should have its dependencies injected (for example in the constructor). The container will inject them (because the container creates all the objects), and the container should be called at the root of your application (the front controller).
Example: you want to inject the Configuration object in a controller:
class MyController {
private $configuration;
public function __construct(Configuration $configuration) {
$this->configuration = $configuration;
}
}
Since that the role of the DIC to create that controller, it will inject the configuration object.
Also, I don't think you should inject the whole configuration object, but just the values you are interested in (but that's another debate).
And also, if you have questions about how to write your controllers, maybe you should read this: Controllers as services?.
I have read the service container chapter in the Symfony2 book multiple times and read SO answers and other resources regarding the topic, but I still just don't seem to get it.
So far, everything I've read has drilled one main truth into my head: the container itself should (practically) never be directly injected into a dependent. This seems to work fine for providing dependencies to other services, but what if an entity in my model wants to inspect the current security context, for instance?
I'm aware that I can implement ContainerAwareInterface and then call setContainer() from a container-aware context to gain access to the container itself in this case, but isn't this the same as injecting the container from the service configuration which is to be avoided at all costs?
What you describe is just bad design. Your model shouldn't be dependent on the service container. If you need to perform some security checks then you would create a service that has the necessary dependencies injected to it and then pass your model objects to it.
By your example it sounds like you're trying to do validation which is described here http://symfony.com/doc/master/book/validation.html and works much like I stated.
i have been trying to get my head round factory patterns and dependency injection and i understand the concepts and basics behind both patterns and that there can be a slight cross over. But before i start coding it up, i just want to check my flow method would be correct.
My intended flow would be...
create a config file with all the properties needed for my 'core classes' in the format
$config['core.classname']['property_name']=$value;
create a factory class that will create an instance of all my core classes and run through the config file injecting the properties in to each class
when my app needs an instance of a class, it uses the factory class to clone the required class which has had its dependencies injected.
As i understand it this would decouple my core classes, allowing for them to be swapped in and out of my code easier.
What you are calling a 'factory' is really more of a dependency injection container. A factory traditionally only creates one type of object.
In general you should avoid creating any core instances until your app actually needs one. You may have 100 core classes defined of which any given app request might only need a couple.
In many cases your app will want to share the same instance or a core class so automatic cloning is probably not quite what you want.
Consider reading through the Service (what you call core) chapter in the Symfony2 framework package for ideas:
http://symfony.com/doc/current/book/service_container.html
I'm trying to pass the entity manager to a service but havent find a correct way yet. I want to complete remove the em from the controller so thats why I'm finding another way.
I was thinking of this options:
1. I could save it in the registry and then try to access it from the service object. can I access the registry from there?
2. Inject the em to a static variable of a base class for the services in the bootstrap.
What is the correct way yo do it?
thanks
I think generally the best way to do it is to pass the entitymanager as an argument to the constructor.
This allows you to easily replace the entitymanager for example when doing unit tests, and unlike your approaches of 1 and 2, it does not depend on behavior in a base class or global data (the registry is a lot like a global variable)
What you could do to avoid touching the EM in your controllers is using a dependency injection container, such as the one in Symfony2 or the one in ZF2 (not sure if that component is very stable yet).
Another perhaps slightly simpler approach would be to have a sort of a "service locator" object, which you would use in the controller to get instances of your services. You could initialize the locator in your bootstrap with the services, or perhaps with a factory class which creates them.
In any case you will probably require at least some kind of an intermediate object in the controller. Personally I don't really see an issue with simply using the EM itself, unless you have some other reasons besides just not wanting to.
There's nothing wrong, IMO, with letting your controllers know about the EM. I typically use a Zend_Application_Resource to bootstrap Doctrine. That resource facilitates a bootstrap resource called "doctrine" which has an EM available. The abstract controller implements and em() method, which returns the EM.
When instantiating service classes, the constructor simply injects the EM via a call to $this->em() at constructor time.
This is nice, as many times, simple controller actions don't need any special service class, but can instead get away with doing $entity = $this->em()->getRepository('Some\Entity')->find(1); In those cases, I don't see any reason for additional redirection via a service class.