Symfony3 – redirect to the previous location

I encountered the following problem:

Given an action deleteAction in a controller I have to redirect to the previous location.

Normally this information would be stored in the request headers as the referrer address.

It’s tempting to write this:

but you’ll have to repeat yourself everytime you do a redirect.

That’s why I decided to move this code to a service:

The requestStack gets injected by Symfony via the service definition:

Next, in the controller just invoke the new service: