Search code examples
laravelroutessegment

Get uri segment in Laravel 5


I'm trying to get uri segments in blade view using Laravel 5. I tried in this way:

{{Request::segment(1)}}

But I'm getting this exception:

Call to undefined method Illuminate\Routing\UrlGenerator::base()

I tried to add:

Illuminate\Routing\UrlGenerator::class,
Illuminate\Contracts\Routing\ResponseFactory::class,

as providers, but what else should I add to aliases?


Solution

  • As has been mentioned, providers may not be the way to do this. It's probably best to get the value you need in the Controller, and then pass it to the view.

    In the controller:

    //In your method
    return response()->view('views.uri', ['uri_segment' => Request::segment(1)])
    

    In the view:

    {{ $uri_segment }}
    

    Let me know if this works for you!