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() }}
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.
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();
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() }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With