Chapters
29 Chapters
|
1:34:37
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: >=5.3.3
Subscribe to download the code!Compatible PHP versions: >=5.3.3
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
21.
Your Very First Service
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
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.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=5.3.3",
"symfony/symfony": "~2.4", // v2.4.2
"doctrine/orm": "~2.2,>=2.2.3", // v2.4.2
"doctrine/doctrine-bundle": "~1.2", // v1.2.0
"twig/extensions": "~1.0", // v1.0.1
"symfony/assetic-bundle": "~2.3", // v2.3.0
"symfony/swiftmailer-bundle": "~2.3", // v2.3.5
"symfony/monolog-bundle": "~2.4", // v2.5.0
"sensio/distribution-bundle": "~2.3", // v2.3.4
"sensio/framework-extra-bundle": "~3.0", // v3.0.0
"sensio/generator-bundle": "~2.3", // v2.3.4
"incenteev/composer-parameter-handler": "~2.0", // v2.1.0
"doctrine/doctrine-fixtures-bundle": "~2.2.0", // v2.2.0
"ircmaxell/password-compat": "~1.0.3", // 1.0.3
"phpunit/phpunit": "~4.1", // 4.1.0
"stof/doctrine-extensions-bundle": "~1.1.0" // v1.1.0
}
}
11 Comments
In symfony 3 it does not work. Fails:
AutowiringFailedException
Cannot autowire service "AppBundle\Reporting\EventReportManager": argument "$em" of method "__construct()" must have a type-hint or be given a value explicitly.
Hey Eduardo Guglielmotti
You are right, in order to use the "auto-wiring" service you have to type-hint your arguments, because the container needs to know which object has to inject.
Can you tell me in which chapter does it happens?
Cheers!
I was trying it with symfony 3.3 following https://knpuniversity.com/s...
regards
Thanks!
I hope you are enjoying our tutorials :)
Have a nice day
Thank you
But I do not know what I've done wrong so it does not work, well in Symfony 3 it's different.
Do you have any idea of my mistake?
You would only have to type-hint your argument, something like this:
Then, the container will do the rest. If you are not using Symfony 3.3, don't forget to register your service and activate the auto-wiring
Hi there!
It's me again :D
I was wondering how can you inject services into Entity Repository.
I made a service for my user's entity in order to handle my custom ID generator and I would like to inject it to my repositories.
Thanks in advance :)
Hi Diego!
You *can* do this, but you shouldn't :). Basically, the repository should *only* have queries. If you're doing something in there that needs an additional service, then it's probably something that no longer belongs in the repository. For these things, I very commonly create "Managers" - e.g. PostManager (if Post is your entity). You can certainly have the PostManager call the PostRepository (just inject the entity manager, I do this all the time).
Hope that helps!
Nice, I'll go that way
Thx :)
Why do we split, the service from the controller? What can i see as the difference between a controller and a service now? That a controller can call a service multiple times depending on what the logic would tell the controller to do, and that the service only does work once for each call?
Hi Oscar!
This is a question that strikes *right* at the heart of the purpose of services :). You could keep your life simple and code 100% in your controller. There are a few disadvantages to this:
1) If you have logic in your controller, it can't be re-used on other pages (i.e. other controllers). This is the *biggest* reason.
2) If you have 50 lines of code in your controller, I need to read them to understand what they're doing. But, if you move them into a separate class - perhaps ImageHelper - and in a method - perhaps createThumbnail() - it becomes a lot more obvious *what* those 50 lines do. Organizing into services starts to naturally organize and self-document your code. This subtle advantage is one of my favorite
3) If you like to unit test, you need to move code into a separate spot.
Moving code from your controller into a service helps with each of these. Does that help?
Cheers!
"Houston: no signs of life"
Start the conversation!