Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get URL segment in Laravel 5

For accessing previous URL in laravel. I am using this code in my controller.

$current_url = Request::url();
$back_url = redirect()->back()->getTargetUrl();
if ($current_url != $back_url) {
  Session::put('previous_url', redirect()->back()->getTargetUrl());
}

This method helps maintainging previous url even when server side validation fails. In my blade I access previous url like this {{ Session::get('previous_url') }}.

I need to find the second segment of my previous url. Thanks

like image 789
Shakun Chaudhary Avatar asked Oct 19 '22 05:10

Shakun Chaudhary


1 Answers

You can do it this way:

request()->segment(2);

request() is a helper function that returns Illuminate\Http\Request, but you can also use the facade Request or inject the class as a dependency in your method.

EDIT

with the redirect back: redirect()->back()->getRequest()->segment(2);

like image 73
thefallen Avatar answered Oct 31 '22 12:10

thefallen