6

I am working in Laravel 5.2 and i want to access URL segments in my controller. I am using

echo  Request::segment(2);

but nothing is print. How can i get values from url in controller.

10
  • Do you get any errors? Commented Jun 8, 2016 at 7:12
  • @Ali After domain "public/admin/edit_country/2". Commented Jun 8, 2016 at 7:14
  • @Chillon not getting any error. Commented Jun 8, 2016 at 7:15
  • You need to you Illuminate\Http\Request fecade. If you are using any other. Commented Jun 8, 2016 at 7:20
  • Ali means; use Request; Commented Jun 8, 2016 at 7:20

2 Answers 2

15

In laravel 5.2 you can do it this way..

echo request()->segment(2);

request() is one of the several helper functions provided in Laravel 5.2. It returns the current request object thus you don't need use statement for the facade on the top of your class.

Sign up to request clarification or add additional context in comments.

2 Comments

This is not an answer, merely an alternative for the way he is doing it right now. Both methods are totally acceptable.
I am using Laravel 5.1 and getting URL data with $request->segment(3) in the controller.
2

In Laravel 7, I am using this to get segments

public function my_function(Request $request )
{
    // By using this, we can get the second segment in route
    // Example: example.com/hh/kk
    
    $segment = $request->segment(2);

    // By using this we will get "kk"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.