Add request parameter to request - symfony

I need to put a flag in an kernel.event_listener at stage kernel.controller in order to do something in an kernel.response-listener.
I thought about adding a parameter to the $request object, however have not found any method or this:
http://api.symfony.com/2.0/Symfony/Component/HttpFoundation/Request.html
What is the best practice to pass some informatino from an kernel.controller-listener to an kernel.response-listener?
Use-case:
We want to set a cookie based on a validation of a specific request attribute (must be in kernel.controller-stage, since based on the result of the validation, the view may behave different).
The cookie can only be set in the kernel.response-listener, since it has the Response-instance.

You can use $request->attributes to pass around information. In your controller listener:
$request->attributes->set('mykey', 'myvalue');
In your response listener:
$myvalue = $request->attributes->get('mykey');

Related

Symfony Firewall checking key in header

Is there a way to check if the header of the request has a key value, compare with another value stored in the env and then give or not the access?
Note: I want to do it inside the firewall in security.yml.
Something like the "guard-authentication" from the docs but I don't need all those methods (getCredentials, getUser, etc... )
I think you are looking for a kernel controller filter, then you can parse the header of the request and search for the required parameter. For the env var you can set it as a parameter as below
//services.yaml
parameters:
your_variable: '%env(YOUR_ENV_VARIABLE)%'
then call it from service container
$var = $container->getParameter('your_variable');
and finally you can use a voter in your controller action to allow/deny access

Should default POJO parameter resolution add the parameter to the model?

I just spent some time troubleshooting an aspect of Spring MVC's default handler method parameter resolution and I'd like to ask those closer to the project if this behavior is intended or if it'd be reasonable to open a ticket suggesting a change.
The issue has to do with the default resolution of POJO-style objects in method parameters like this:
#RequestMapping("/endpointwithparams")
public String endpointWithParams(EndpointParams params) {
// Do some stuff
return "viewname";
}
With no annotations or custom argument resolvers, Spring will attempt to bind the EndpointParams object by matching request parameters to its field names. It will even run validators if any are configured. This seems great - it lets me write simple POJO objects to organize related sets of parameters without having to have a custom argument resolver for each one.
The part that throws me off is that after the EndpointParams object is created it will also be automatically added to the model. This is because the actual resolver of this parameter will be a ModelAttributeMethodProcessor with its "annotationNotRequired" flag set to true. I don't want this parameter added to the model - its presence causes some trouble down the line - and it certainly wasn't intuitive to me that I should expect that addition to happen for a parameter that wasn't annotated with #ModelAttribute.
This behavior is also inconsistent with what happens when you have a "simple" request parameter like this:
#RequestMapping("/endpointwithparams")
public String endpointWithParams(String param) {
// Do some stuff
return "viewname";
}
In the above example, the String param will be resolved by the RequestParamMethodArgumentResolver, which will not add anything to the model.
Would it be reasonable to suggest that better default logic for non-annotated POJO parameters would be the same binding and validation that currently occurs, but without the automatic addition to the model? Or is there some context I'm missing that makes the full #ModelAttribute behavior the best default choice?

Modify Value of Parameters of Request

I want to know if it is possible to modify the value of the parameter of the request.
But i don't know how to do this.
I try with
$requestContent = $this->getRequest()->request->get('tactill_customerbundle_customertype');
Next I use
$request->request->replace()
But I don't how to use this method in my case.
Thanks
The replace method replaces all of the parameters in the request, so you probably do not want to do that.
I would use the set method instead - So you can do:
$request->request->set('tactill_customerbundle_customertype', $newValue)
You can read more in the Symfony2 documentation (http://api.symfony.com/2.0/) - you are looking for Symfony\Component\HttpFoundation\Request (which is the $request variable), which then returns a Symfony\Component\HttpFoundation\ParameterBag when you call the request() method.

How to get request type (master/sub) in Symfony2 controller?

Is there possible get request type in controller? How?
To detect if the request is a master or not requires the use of the RequestStack, which should be injected into your controller. The request stack has 3 useful methods
getCurrentRequest();
getMasterRequest();
getParentRequest();
The getParentRequest() will always return null if the current request is the master.
I was looking for this myself, and it seems it is just passed around, so there doesn't seem to be one single place that knows what it is.
My thought for solving this would be to create a simple kernel.request listener that just adds an attribute to the request. Rough (un-tested) code below:
public function onKernelRequest(GetResponseEvent $event)
{
$event->getRequest()->attributes->set('_request_type', $event->getRequestType());
}
Then in the controller you should be able to do:
$requestType = $this->getRequest()->attributes->get('_request_type');
Again this is untested. You would need to write out the full listener class and add it to the services config file, but other than that I think this will work.
Easy, just call the getMethod() method on your Request object:
$method = $this->get('request')->getMethod();
This will return the HTTP method of the current request, e.g. GET, POST, PUT or DELETE.

How do i provide a custom session ID getter/setter in asp.net

I want to pass the sessionID as a json parameter, I can see how to override SessionIDManager and such, but this just covers custom creation/validation of sessionID's as opposed to where it actually gets the sessionID from.
There is of course 'cookieless' which puts it in the url, but that doesn't work for me either.
So i'd like to override the session handling so I can specify where it should look for the sessionID.
http://msdn.microsoft.com/en-us/library/system.web.sessionstate.sessionidmanager.aspx

Resources