Stanley Ngumo
Stanley Ngumo

Reputation: 4239

Modify Url in Request Object in Symfony2

I am trying to modify the Url entering the Symfony kernel but leaving all the others unchanged. Have tried usinh Request::create but that method replaces all other variables in the request.

How can this be done?

Upvotes: 4

Views: 2595

Answers (2)

adaz
adaz

Reputation: 66

I found this via search... i dont know if this was working on symfony 2, but on symfony 5, this would replace the initial array having all server params, with the only one param 'REQUEST_URI'.

The alternative (Symfony 5) to keep all other keys:

$serverBag = $request->server;
$requestUri = $serverBag->get('REQUEST_URI');
$serverBag->set('REQUEST_URI',str_replace('/oldUrl', '/newUrl', $requestUri));
$request = $request->duplicate(null, null, null, null, null, $serverBag->all(), null);

Upvotes: 4

Stanley Ngumo
Stanley Ngumo

Reputation: 4239

Continued to do some research and found that it can be done using the duplicate method. Example:

$new_url = $request->duplicate(null, null, null, null, null, array('REQUEST_URI' => $old_url, null));

Hope this is helpful

Upvotes: 2

Related Questions