Skip to content

Cookbook adding after filter using kernel.response listener (issue #649) #1653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 9, 2012

Conversation

ricardclau
Copy link
Contributor

This is the second part on the cookbook about setting before and after filters using Symfony2 Event Listeners. Sorry for the BIG delay about it, but as they say better later than never :)

Any typos and errors detected will be more than welcome!

Best regards


So, after generating a JSON response, this hash has to be added to the response.

Before and after filters with kernel.controller / kernel.response events
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it better to place this in a inline code block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry I don't understand what you mean here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ricardclau I mean something like this:

Before and after filters with ``kernel.controller``/``kernel.response`` events

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sure, sorry :)

@stof
Copy link
Member

stof commented Aug 26, 2012

Your example is a bad one IMO. If you use a custom response class, you can do this without using a listener (simply overwrite setContent)

@ricardclau
Copy link
Contributor Author

@wouterj I have 2 doubts regarding your catchs, comments added after yours

@stof Note that I am using the token in the request as a salt for the sha1. I don't think this can be cleanly done overwriting setContent. Moreover, the idea of using the Listener is not just for a simple hash, it is to make people aware that another "hashing service" could be injected and do some more complicated stuff after creating and returning the Response in the controller.

Anyway, if there are more -1 I can revert this, but I still think it is not a bad example. Any opinions?

@stof
Copy link
Member

stof commented Aug 27, 2012

@ricardclau then I would use an example which does not use a custom Response class, as I'm quite sure some people will think that this pattern is not usable without doing it too.

@ricardclau
Copy link
Contributor Author

@stof I am using a custom Reponse class as a way to "tag" it, so that changing the response only gets executed in that type of responses and not all of them (actually analog to the before filter example). The pattern is usable without extending Response but it would get executed for all Responses and you don't want that most of the times I guess.

Anyway, I am open to change any thing you want to make the example more useful, so I am open to suggestions!

weaverryan added a commit that referenced this pull request Sep 9, 2012
@weaverryan weaverryan merged commit 723d9b3 into symfony:2.0 Sep 9, 2012
@weaverryan
Copy link
Member

Hi guys!

I really like this entry - takes a fairly non-technical question of how to have before/after filters and gives it a technical answer. I did make one change where I separated the before and after filters into 2 different sections to be clear about how each piece works independently. I also changed how we flagged a request to be hashed by using the request's attributes bag instead of the custom Response. Again, this was just to try to "minimize" things as much as possible so that the true important parts of the entry standout.

If anyone has any thoughts, fixes or complaints with this final version, please let me know!

Thanks Ricard for finishing up this entry!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants