获取 Laravel 5 中的 uri 段

Get uri segment in Laravel 5

我正在尝试使用 Laravel 5 在 blade 视图中获取 uri 片段。 我这样试过:

{{Request::segment(1)}}

但是我遇到了这个异常:

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

我尝试添加:

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

作为提供商,但我还应该向别名添加什么?

如前所述,提供程序可能不是执行此操作的方法。最好在Controller中获取你需要的值,然后传递给视图。

在控制器中:

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

在视图中:

{{ $uri_segment }}

让我知道这是否适合您!