Search code examples
phplaravellaravel-5string-comparison

How to compare Laravel's url()->previous() with url()->current()


It seems Laravel's url()->previous() and url()->current() is broken. I can't do a if previous == current, because previous outputs with http:/https: and ends with /, and current outputs without this.

I thought this was not going to be a problem. I could just prepend http:/https: to the url()->current() and move on with my life, but apparently it was not going to be that simple.

The problem: $url_1 != $url_2

Code:

$url_1 = url()->previous();
$url_2 = url()->current();

var_dump( $url_1 );
var_dump( $url_2 );

Output:

string(22) "http://localhost:3000/"
string(21) "//localhost:3000"

Attempt to fix:

Code:

$url_1 = url()->previous();
$url_2 = 'http:' . url()->current() . '/';

var_dump( $url_1 );
var_dump( $url_2 );

Output:

string(22) "http://localhost:3000/" 
string(27) "http://localhost:3000/" 

So $url_1 is still not equal to $url_2. Does anyone know what's going on here? Seems to me that http: is being treated as a single character.

How can I actually compare the two, because as it is now, $url_1 is never going to be equal to $url_2...


Solution

  • I found that previous() in Illuminate\Routing\UrlGenerator uses getPreviousUrlFromSession() which returns the result of this: session()->previousUrl().

    Comparing session()->previousUrl() with url()->current() actually works. Though I'm still curious as to why the first method doesn't work.