Chapters
23 Chapters
|
2:22:24
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.4.1
Subscribe to download the code!Compatible PHP versions: ^7.4.1
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
15.
Automatic Controller Queries: Param Converter
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.
This tutorial also works great for Symfony 6!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.4.1",
"ext-ctype": "*",
"ext-iconv": "*",
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/doctrine-bundle": "^2.1", // 2.1.1
"doctrine/doctrine-migrations-bundle": "^3.0", // 3.0.2
"doctrine/orm": "^2.7", // 2.8.2
"knplabs/knp-markdown-bundle": "^1.8", // 1.9.0
"knplabs/knp-time-bundle": "^1.11", // v1.16.0
"sensio/framework-extra-bundle": "^6.0", // v6.2.1
"sentry/sentry-symfony": "^4.0", // 4.0.3
"stof/doctrine-extensions-bundle": "^1.4", // v1.5.0
"symfony/asset": "5.1.*", // v5.1.2
"symfony/console": "5.1.*", // v5.1.2
"symfony/dotenv": "5.1.*", // v5.1.2
"symfony/flex": "^1.3.1", // v1.21.6
"symfony/framework-bundle": "5.1.*", // v5.1.2
"symfony/monolog-bundle": "^3.0", // v3.5.0
"symfony/stopwatch": "5.1.*", // v5.1.2
"symfony/twig-bundle": "5.1.*", // v5.1.2
"symfony/webpack-encore-bundle": "^1.7", // v1.8.0
"symfony/yaml": "5.1.*", // v5.1.2
"twig/extra-bundle": "^2.12|^3.0", // v3.0.4
"twig/twig": "^2.12|^3.0" // v3.0.4
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.3", // 3.4.0
"symfony/debug-bundle": "5.1.*", // v5.1.2
"symfony/maker-bundle": "^1.15", // v1.23.0
"symfony/var-dumper": "5.1.*", // v5.1.2
"symfony/web-profiler-bundle": "5.1.*", // v5.1.2
"zenstruck/foundry": "^1.1" // v1.5.0
}
}
7 Comments
Completely bluffed by this Param Converter possibilities. Is it not more demanding though, performance-wise, than to do a simple query on the slug ? I don't really know what's behind, but I am curious :)
Hey Hugo,
ParamConverter do the same thing as you why do it manually: takes the slug, get the repository, and find the entity by slug throwing an 404 exception if not found. As a result, when you use this param converter feature - you write less code, and so your controller remains more clean and easy to read. No any performance impact in theory, but you always can do some performance tests to know for sure ;)
Cheers!
Since the lessons for Symfony 6 hasn't got this far yet, I've been working through this batch using my Symfony 6 installation. Not too many issues with it but I notice @5:10, points 3 and 4 don't appear to be true anymore. Any idea why?
I can't autowire
entity. Shows error I have added below. Please help.. I am stucked
Cannot autowire argument $question of "App\Controller\QuestionController::show()": it references class "App\Entity\Question" but no such service exists.
Hey Jewel F.
You may be missing some configuration in your route. Check this post about how to use the param converter annotations https://symfony.com/doc/cur...
Cheers!
I've found the ParamConverter to be a good solution when instead of typehinting with the entity class itslef, an interface is used as a type hint. So I wrote a param converter that follows the
doctrine.orm.resolve_target_entitiesvalue.Is there any other better way to do this?
Hey facundo,
Do your entities implement interfaces? :) Do you have a few entities that should have the same method signatures in it? If so, yes, it makes sense to use interfaces, but then how would you know which exactly entity to query if a few entities are implementing the same interface? :)
But every your entity implements a separate unique interface - then it's overkill, and you use interfaces incorrectly :)
It's difficult to imagine your setup for me, so difficult to advice something. But still, typehinting real entity class names is probably the best solution here, simple and robust.
Cheers!
"Houston: no signs of life"
Start the conversation!