09.
Autowiring Controller Arguments
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
4 Comments
Hey Ryan,
What about the DataFixtures Service?
Hey DaveKnifeman!
Excellent question! Back when we made this tutorial, your fixtures were *not* services: the DoctrineFixturesBundle "found" them by looking in the DataFixtures/ORM directory if your bundle and then it instantiated them all on its own. This mean that you were not allowed to have any constructor arguments (quite like how controllers were done before these changes). However, in the latest version of the bundle, your fixture classes *are* services. Here's how it works:
1) Thanks to our code in services.yml, our fixture classes are registered as services just like everything else
2) In the latest version of the bundle, as long as you extend the Fixture class from the bundle (https://github.com/doctrine... - or more specifically, as long as your fixture class implements the ORMFixtureInterface, the "autoconfigure" feature will automatically apply a special "tag" to your fixture services
3) When you run doctrine:fixtures:load, instead of locating your fixture classes based on some directory, it loads all services that have the above "tag". And instead of instantiating your fixtures, they are created by the container.
4) Because your fixtures are now services, you can use normal dependency injection if you need any dependencies.
Let me know if you have any issue putting this all together in practice. Fixtures are MUCH cooler to work with now :).
Cheers!
Hey guys,
can we also autowire $this->get('security.authentication.guard_handler') in the Controller?
Hey Chris,
Yes, we can! You just need to find a proper class name to typehint. To know the proper class name run:
bin/console debug:autowiring
And search for "security.authentication.guard_handler". I see you can autowire it with Symfony\Component\Security\Guard\GuardAuthenticatorHandler typehint.
Cheers!
"Houston: no signs of life"
Start the conversation!