在 Laravel 5 中获取 uri 段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34053369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:45:55  来源:igfitidea点击:

Get uri segment in Laravel 5

laravelroutessegment

提问by mistery_girl

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

我正在尝试使用 Laravel 5 在刀片视图中获取 uri 段。我以这种方式尝试过:

{{Request::segment(1)}}

But I'm getting this exception:

但我得到这个例外:

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

调用未定义的方法 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?

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

回答by Josh Rumbut

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.

如前所述,提供者可能不是这样做的方式。最好在 Controller 中获取您需要的值,然后将其传递给视图。

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!

让我知道这是否适合您!