0

I was going through some code in symfony, and I found

$request->request->replace()

Actually, a form is posted and its value is fetched in a function say,

public function someFunction(Request $request){
    $data = $request->request->all() ? : json_decode($request->getContent(), true);
    $request->request->replace($data);
}

When I dumped,

$request->request->replace($data)

The result is null. I didn't understand why is it used and what are its benefits?

I searched about it, some say it is used to sanitise the data, some say we should not use it as replaces all of the parameters in the request instead we should use set method.

And I did not get any of it as I am new to symfony.

What does $request->request->replace() does with the parameter provided to it?

Saurabh
  • 432
  • 1
  • 10
  • 23

1 Answers1

3

Your $request is an instance of Symfony\Component\HttpFoundation\Request . Using $request you have access to properties such as request, query, cookies, attributes, files, server, headers. Each of these properties is of type Symfony\Component\HttpFoundation\ParameterBag. Instance of ParameterBag provides access to request parameters using method $request->request->all(). This method will return 'parameters' property of ParameterBag instance.

The $request->request->replace($data) will set 'parameters' property in ParameterBag instance to $data.

Also replace() method does not have any return type that's why when you dumped $request->request->replace($data) you got null as output.

If you want to add some extra parameters to your request then replace() is not the right choice rather you should use set() method in ParameterBag.

Nishant Middha
  • 302
  • 1
  • 9
  • What about sanitisation of data? – Saurabh May 27 '19 at 06:18
  • 1
    @Saurabh according to me sanitisation is not the correct term to use. Suppose you want to pass your request parameters to some other service after some modification then you will fetch existing parameters using **all()** method and modify them accordingly. After that replace existing parameters with the modified one using **replace()** method. – Nishant Middha May 27 '19 at 06:51