Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go back URL in Laravel 5.1

How can I get the previous URL visited on the website in Laravel 5.1?

In Laravel 4 I just needed to write it like below:

{{ URL::previous() }} 
like image 874
Linus Odenring Avatar asked Sep 28 '15 15:09

Linus Odenring


People also ask

How do I redirect a previous URL?

There are two approaches used to redirect the browser window back. Approach 1: Using history. back() Method: The back() method of the window. history object is used to go back to the previous page in the current session history.

How do I redirect back to original URL after successful login in laravel?

You can apply this filter to the routes that need authentication. Route::filter('auth', function() { if (Auth::guest()) { return Redirect::guest('login'); } }); What this method basically does it's to store the page you were trying to visit and it is redirects you to the login page. return Redirect::intended();


1 Answers

The cleanest way seems to be using the url() helper:

{{ url()->previous() }} 

URL::previous() works for me in my Laravel 5.1 project. Here is Laravel 5.1 doc for previous() method, which is accessible through URL Facade.

You can still try alternatives, in your views you can do:

{{ redirect()->getUrlGenerator()->previous() }} 

or:

{{ redirect()->back()->getTargetUrl() }} 
like image 110
Mustafa Ehsan Alokozay Avatar answered Oct 15 '22 12:10

Mustafa Ehsan Alokozay