URL::Previous() returns incomplete path

Asked

Viewed 1,081 times

8

How I’m studying the book Code Bright, from time to time there are some errors that I take a long time to resolve. However, I couldn’t find a solution in Google.

By doing the Redirect::to() from one route to another the previous full address should be displayed, but only my URL appears vhost without the remainder of the route.

Code Bright book code:

Route::get('first', function()
{
    // redirect to the second route
    return Redirect::to('second');
});

Route::get('second', function()
{
    return URL::previous();
});

When I walk into the endereço/first and I’m redirected to the route endereço/second, should appear written endereço/first but only address appears.

Question

What would be the right solution to solve the return url print?

Detail, I checked on github that as far as I understood the Redirect::to generates a 302 call that does not transmit the information from where the route request is coming from. Perhaps you are wrong about this.

  • 5

    Just to let you know, tags are used to help locate questions. They are even used to help index on Google. It’s made in a very clever way. So the name in the title doesn’t help the search, Having one isn’t just a taste.

1 answer

4


The method URL::previous() is based on HTTP Referer which is roughly an information your browser sends to the site being accessed in the request header (HTTP Headers), informing which URL the user was at the time of the request.

This behavior of sending the Referer is not homogeneous and your browser may not be sending this information.

To check the header HTTP Referer was sent or not, you can do as follows:

//  PHP "puro"
echo $_SERVER['REFERER'];

// Laravel
echo Request::header('referer');

If the user has the page reloaded, this information will be lost, it only exists if the user is accessing the page through a Link (<a />)

Best practice is to implement a mechanism that stores the current page in session, such as this:

// Use como filtro, antes das rotas, ou em seu controller, 
// o importante é executar o código em todas as páginas
if(Session::get('url_atual')) {
    Session::put('url_anterior', Session::get('url_atual'));
} 
Session::put('url_atual', URL::current());

And then, if you need to create a link or redirect to the previous page, just use:

Redirect::to(Session::get('url_anterior'));

Nota1: the above example is nut implemented, use it cautiously in a production system.


Note 2: See Method Implementation in: https://github.com/laravel/framework/blob/master/src/Illuminate/Routing/UrlGenerator.php lines 72 to 82:

public function previous()
{
    return $this->to($this->request->headers->get('referer'));
}
  • Yes, but this does not solve the return URL problem. When required to print the URL from where the redirect came from it does not return what I need but only the domain of the site. Moreover the doubt was not with respect to routing but rather request the URL from where it came from. https://meusite.com/first in making URL::previous() I hope he returns me this address with the route path.

  • in the question you indicate that you make use of Redirect::to and not URL:Previous()

  • but I will edit the answer to try to help you, which web server do you use? apache + php normal?

  • apache + php default. Reply to $ apachect -v: erver version: Apache/2.2.24 (Unix)&#xA;Server built: Jul 7 2013 18:05:17. Response to $ php -v: PHP 5.4.21 (cli) (built: Oct 21 2013 13:24:34) &#xA;Copyright (c) 1997-2013 The PHP Group&#xA;Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies&#xA; with Xdebug v2.2.2, Copyright (c) 2002-2013, by Derick Rethans.

  • @Thiagoprado create the following route, show the result of Request::headers(); please

  • @Thiagoprado I changed the answer, see if it helps now!

  • Thank you very much, it worked right. I would ask if possible that you demonstrate the best way of implementation for this case. As you said that this implemented in a nut way what would be the best way?

  • Yes @Thiagoprado, have something similar I will share here as soon as you give a time.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.