Search code examples
phpsymfonyhttp-redirectroutesreferer

Return to referer (double back)


I often use referer when i want to redirect to the actually page :

$referer = $request->headers->get('referer');
return $this->redirect($referer);

But when I use this method I have to hit twice to the backward button (the button of any browser that will go to the last page used) ... So I want to know if there is the other method to redirect to the same page but without this double backward bug ... thanks for anyone that will help :)

I am talking about this button ---> enter image description here


Solution

  • Insted of use the referer, which could not be a secure method, you can:

    1) Use some of the redirects methods provided by Symfony (docs):

    return $this->redirectToRoute('homepage');
    

    In case you don't really know what is your referer page, you can still pass the desired route as parameter to your controller, and use it in your redirectToRoute call.

    2) Use JavaScript.

    function goBack() {
        window.history.back();
    }
    

    Probably, you need to execute some back-end operations before goBack method is executed, if is the case, you can use AJAX calls, and execute goBack method when servers responds.