This tutorial is built using Symfony 4, but most of the concepts apply fine to Symfony 5!
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"doctrine/annotations": "^1.8", // v1.8.0
"knplabs/knp-markdown-bundle": "^1.7", // 1.7.0
"knpuniversity/lorem-ipsum-bundle": "*@dev", // dev-master
"nexylan/slack-bundle": "^2.0,<2.2", // v2.0.1
"php-http/guzzle6-adapter": "^1.1", // v1.1.1
"sensio/framework-extra-bundle": "^5.1", // v5.1.6
"symfony/asset": "^4.0", // v4.0.6
"symfony/console": "^4.0", // v4.0.6
"symfony/flex": "^1.0", // v1.21.6
"symfony/framework-bundle": "^4.0", // v4.0.6
"symfony/lts": "^4@dev", // dev-master
"symfony/twig-bundle": "^4.0", // v4.0.6
"symfony/web-server-bundle": "^4.0", // v4.0.6
"symfony/yaml": "^4.0", // v4.0.6
"weaverryan_test/lorem-ipsum-bundle": "^1.0" // v1.0.0
},
"require-dev": {
"easycorp/easy-log-handler": "^1.0.2", // v1.0.4
"sensiolabs/security-checker": "^4.1", // v4.1.8
"symfony/debug-bundle": "^3.3|^4.0", // v4.0.6
"symfony/dotenv": "^4.0", // v4.0.6
"symfony/maker-bundle": "^1.0", // v1.1.1
"symfony/monolog-bundle": "^3.0", // v3.2.0
"symfony/phpunit-bridge": "^3.3|^4.0", // v4.3.3
"symfony/stopwatch": "^3.3|^4.0", // v4.0.6
"symfony/var-dumper": "^3.3|^4.0", // v4.0.6
"symfony/web-profiler-bundle": "^3.3|^4.0" // v4.0.6
}
}
8 Comments
Hi! I'm using Symfony 5.2.
I have a notice about "The Easy Way" (
<argument type="tagged" tag="knpu_ipsum_word_provider" />). I had the following configuration inKnpULoremIpsumExtension::load():And the following configuration in "services.xml":
Words from
CustomWordProviderweren't present on page. So, I still should or add tag definition for provider in "services.yaml" in my main app:... either set tag in
KnpULoremIpsumExtension::load():Maybe, I've done something wrong, but only explicit tag definition for
App\Service\CustomWordProviderworked for me (<i>in case of "easy way"</i>).Hey @Boolean_Type!
Hmm, let's see. So first, as you know, one way or another we must to add the
knpu_ipsum_word_providertag to theCustomWordProviderservice. But, there are two ways to do this... and you listed both of them :). Either you can add the tag directly, or you can register the interface it implements for autoconfiguration. How you add the tag doesn't matter - it will work the same both ways. We actually do theregisterForAutoconfiguration()thing - we do it in the next chapter: https://symfonycasts.com/screencast/symfony-bundle/tags-compiler-pass#codeblock-1ed6575410Cheers!
Hi!
Am I correct saying that autoconfiguring tags does not work for bundle services since there's a comment in code snippet at the given link like
? In my application I have an interface with auto-configured tag in services.yaml. And my bundle service implementing that interface does not have a tag on it until I explicitly add the tag to bundle's services.xml.
Hey erop!
That's 100% correct! The reason is that the
instanceofstuff (just like thedefaults) stuff applies to only the current file that this code appears in. This was done on purpose: it's a powerful way to make your life easier, but if it affected your entire application, crazy things would happen :). You could add aninstanceofin a bundle that you're creating. But typically, as a best practice, bundle should explicitly require everything: the "shortcuts" are meant to help application developers more than developers of re-usable code.Cheers!
Thank you, Ryan!
Is it possible that now this argument is not an array but an object of type RewindableGenerator?
<argument type="tagged" tag="knpu_ipsum_word_provider"/>
That's what I get when I dump $wordProviders in the constructor of class KnpUIpsum. :-/
It does work when you remove the typehint "array $wordProviders"
Hey Laurens M.!
Hmm. I can't find a change in Symfony that would specifically cause this. However, from checking the code, it looks like this has always been the case - even the original blog post about this feature shows using the
iterabletype-hint, not array: https://symfony.com/blog/new-in-symfony-3-4-simpler-injection-of-tagged-servicesSo, we'll add a note about that! When I created this, I was thinking that you could simply use the
taggedargument type and make no other changes to your code. But, it looks like the array type-hint does need to change to iterable.Thanks for bringing this up!
Thanks Ryan, iterable as typehint indeed works like a charm. I appreciate the quick and clear response, all the bests!
"Houston: no signs of life"
Start the conversation!