In Symfony 2, I have a controller which has many methods, I want to be able to wrap methods within all the methods of the controller, else I would have to manually place the code at the end of each function. Has someone ever implement this in Symfony 2?
If your code concern Response, have a look at kernel.view event.
Ex. https://stackoverflow.com/a/8805462/520114
Otherwise, see other events.
Related
Hi I have a question about symfony application architecture,
In my application I create different user, but when a user is created, updated, deleted, or his picture change, I need to do some action.
What is the best way to do this ? I excluded to do this on a controller action. There is 2 others solutions :
Create differents events like user.created, user.updated, ... And dispatch it on the controller action and make different listener to do the different action like MailListener (for user.created) TaskListener (for user.created) for add a task.
Use a service like UserManager and on this service have a method like userCreated() and on this method call differents actions like sendMailOnCreated, addTaskOnCreated for example.
For you what is the best method ?
For me, your first solution is the best one. It's clearly a use case for the Event component. It will be easier to maintain and more readable.
Moreover, if you need to add more listener you just need to create another one and bind it to your event. You don't need to modify your controller anymore.
I'm writing phpspec test for controller
In action I am calling another method with this:
$this->getDoctrine()->getManager()->createQueryBuilder();
I cannot pass this in phpspec.
What I'm getting is
method Double\ObjectManager\P7::createQueryBuilder() is not defined
Have you got any suggestion how to spec it?
If you can't spec something, it's a indicator you have a bad design. In your case you should never create query builders in your controllers. Controller should be only a clue between different services.
For doctrine queries create repositories!
I want to execute a function when a road is called it in any controller but before the call to action.
The goal is that the code is invisible in the controller and do it the best walkthrough possible.
Here you have the solution: https://matt.drollette.com/2012/06/calling-a-method-before-every-controller-action-in-symfony2/
In short, you have to intercept the call to a Controller using a Kernel event, and then execute your code inside the listener. You can "select" which controllers use this behavior extending a particular Interface.
I'm wondering if there is an ASP.Net MVC equivalent to PHP's require_once() function.
Lets say I need to call RenderAction to a particular action twice. However, inside the view that is rendered from said action, I need to print out some init scripts but only once.
Is there an easy mechanism to do this?
Should a flag be set in the controller and then passed into the view?
Thanks for any help you can provide.
Edit
Thanks for the responses. I know I can do this through the controller or temp data or some mechanism like that but wasn't sure if there was anything built into .NET MVC that would have done this for me automatically.
I am not aware of such equivalent.
Should a flag be set in the controller and then passed into the view?
That seems like a good way. The flag needs to be passed as argument to the controller action when using the RenderAction helper.
I made a custom handler that derives from MvcHandler. I have my routes using a custom RouteHandler that returns my new handler for GetHttpHandler(), and I override ProcessRequest() in my custom handler. The call to GetHttpHandler is triggering a breakpoint and my handler's constructor is definitely being called, but BeginProcessRequest() is being called on the base MvcHandler instead of ProcessRequest().
Why are the async methods being called when I haven't done anything to call them? I don't want asynchronous handling, and I certainly didn't do anything explicit to get it. My controllers all derive from Controller, not AsyncController.
I don't have the source code with me right now, but I can add it later if needed. I was hoping someone might know some of the reasons why BeginProcessRequest might be called when it's not wanted.
Brad Wilson responded to my post on the Asp.net forums with the following answer http://forums.asp.net/t/1547898.aspx:
Short answer: yes.
With the addition of AsyncController,
the MvcHandler class needs to be an
IHttpAsyncHandler now, which means
that as far as the ASP.NET core
runtime is concerned, the entry points
are now BeginProcessRequest and
EndProcessRequest, not ProcessRequest.
It sounds like ProcessRequest is not even called anymore, but I could be mistaken. I can say that I haven't seen it in my testing.