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
}
}
17 Comments
May I ask you why do you use a separate storage for event names? Why using Event::class, or at least, Event::getName() or Event::EVEN_NAME is not the preferred approach? I understand, it's seducing to keep the list of the Bundle Event Names in one, separate place, but, by doing this you
1) make the code less straight-forward, just compare this:
<blockquote>return [
];</blockquote>
and this
`return [
];`
2) actually double the code by adding Event's class namespace and description to @Event annotation. Like, if you alter the Event's functionality, besides the Event doc, you will also have to update the @Event annotation - one change, but double places.
just as you want :D
https://symfony.com/blog/ne...
Hey Yaroslav Y.
That's a great observation, and the reason is that this was not done regionally in Symfony because the ClassName::class keyword didn’t exist back then, but these days, yes, we should start making the event name just the class name. Probably someday Symfony will change it.
Cheers!
But how we assign AddMessageToIpsumApiSubscriber on event dispatcher?
I have my Subscriber class implemented with EventSubscriberInterface
Then create
` $event = new FilterApiResponseEvent($data);
I have <b>eventDispatcher</b>, I have $<b>event</b> but it ignore my subscriber. Method <b>getSubscribedEvents()</b> don't call.
Hello, I had the same issue and that was in my FilterApiResponseEvent I was extending Symfony\Contracts\EventDispatcher\Event instead of
Symfony\Component\EventDispatcher\Event;
I also clear my var/cache in my Bundle
I write it for future users
Hey triemli!
Excellent question! Normally (in your application) all you need to do is create a class that implements EventSubscriberInterface and... boom! Symfony magically sees it and registers it with the event dispatcher. That is thanks to "autoconfigure: true", which we don't normally use in bundles (since we prefer to configure everything explicitly.
So, if you're registering an event subscriber in your bundle, you'll need to give it a tag:
kernel.event_subscriber- something like:Cheers!
Hah after register in service.xml it works. But on you we didn't register, isn't?
Hey triemli
You're right. Ryan didn't configure the event subscriber inside the bundle and it works because the main application has autoconfiguration enabled.
Cheers!
Hm. THat's weird, because I had autoconfiguration is true too
Interesting... what Symfony version are you using? Maybe that "recently" changed
I just download Course code and made composer install.
Hmm, so you're on Symfony4.0
I'm not sure now but it's recommended to configure your bundle's services explicitly, assuming that the main application won't have autoconfigure enabled
Hello there,
Full disclosure I might be a little OCD and I HATE deprecation warnings. So... I tried to make the bundle as backward compatible as possible using Symfony 4.4.
So I extended my FilterApiResponseEvent with Event from Symfony\Contracts\EventDispatcher\Event. But now, my Spacebar is crashing since this class doesn't exists in 4.0. My question is, what would be the proper way to make it backward compatible but with no deprecation warnings when using 4.4?
Eg. for my config I used that nice little trick.
` $treeBuilder = new TreeBuilder('knpu_lorem_ipsum');
But I don't how to go about my current Event problem.
Thank you!
Julien
Hey julien_bonnier!
Ha! No problem. Actually, you should probably be a little OCD about deprecations in a bundle: if your bundles triggers deprecations, you will hear about it from your users ;).
About the events, there may be a more clever solution than this, but here is what comes to mind:
That should do it. You will have to duplicate the inside of the event, but hopefully that's not too much (there may also be a clever way around that that I'm not thinking about). Supporting multiple versions of Symfony like this is super hard - it's tricky for me, even though I have a fair amount of experience. And it often requires you to write weird code that would never otherwise be "ok" ;). This is also why it's important to have your test suite run against your normal dependencies but also against your "low" dependencies (e.g. https://github.com/SymfonyCasts/reset-password-bundle/blob/96391ea830a24bc2dcfc4437edd6648cc55ebcfd/.github/workflows/ci.yml#L184 ).
Let me know if it helps!
Cheers!
I see, I didn't think of it, and I don't like to duplicate code, but I understand that sometimes, when you want your code to support different versions of a framework you have to do some acrobatics. Any way thanks for the answer.
Cheers
Haha, indeed! When it comes to writing reusable code... and supporting different versions of Symfony or PHP... things can get INSANE. The rules are quite different :). And that's kind of the goal of many open source authors: they do a lot of the weird & hard work, so that application developers have an easy life. Welcome to the dark side ;)
Cheers!
"Houston: no signs of life"
Start the conversation!