使用 Blade 获取 Laravel 5 中当前 URL 的最后一部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37907191/
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
Get last part of current URL in Laravel 5 using Blade
提问by Ronald Zwiers
How to get the last part of the current URL without the /
sign, dynamically?
如何动态获取没有/
符号的当前 URL 的最后一部分?
For example:
例如:
In www.news.com/foo/bar
get bar
.
在www.news.com/foo/bar
获取bar
.
In www.news.com/foo/bar/fun
get fun
.
在www.news.com/foo/bar/fun
获取fun
.
Where to put the function or how to implement this in the current view?
在当前视图中将函数放在哪里或如何实现它?
回答by user2094178
Of course there is always the Laravel way:
当然,总有 Laravel 的方式:
request()->segment(count(request()->segments()))
回答by JRoss
This is how I did it:
我是这样做的:
{{ collect(request()->segments())->last() }}
回答by Erik Berkun-Drevnig
Use basename()
along with Request::path()
.
使用basename()
沿Request::path()
。
basename(request()->path())
You should be able to call that from anywhere in your code since request()
is a global helper function in Laravel and basename()
is a standard PHP function which is also available globally.
您应该能够从代码中的任何位置调用它,因为它request()
是 Laravel 中的全局辅助函数,并且basename()
是一个标准的 PHP 函数,它也可在全局范围内使用。
回答by yama
You can use Laravel's helper function last
. Like so:
你可以使用 Laravel 的辅助函数last
。像这样:
last(request()->segments())
last(request()->segments())
回答by Qevo
The Route objectis the source of the information you want. There are a few ways that you can get the information and most of them involve passing something to your view. I strongly suggest not doing the work within the blade as this is what controller actions are for.
该路线的对象是你想要的信息的来源。有几种方法可以获取信息,其中大多数都涉及将某些内容传递给您的视图。我强烈建议不要在刀片内进行工作,因为这是控制器操作的目的。
Passing a value to the blade
将值传递给刀片
The easiest way is to make the last part of the route a parameter and pass that value to the view.
最简单的方法是将路由的最后一部分作为参数并将该值传递给视图。
// app/Http/routes.php
Route::get('/test/{uri_tail}', function ($uri_tail) {
return view('example')->with('uri_tail', $uri_tail);
});
// resources/views/example.blade.php
The last part of the route URI is <b>{{ $uri_tail }}</b>.
Avoiding route parameters requires a little more work.
避免路由参数需要做更多的工作。
// app/Http/routes.php
Route::get('/test/uri-tail', function (Illuminate\Http\Request $request) {
$route = $request->route();
$uri_path = $route->getPath();
$uri_parts = explode('/', $uri_path);
$uri_tail = end($uri_parts);
return view('example2')->with('uri_tail', $uri_tail);
});
// resources/views/example2.blade.php
The last part of the route URI is <b>{{ $uri_tail }}</b>.
Doing it all in the bladeusing the request helper.
使用请求助手在刀片中完成所有操作。
// app/Http/routes.php
Route::get('/test/uri-tail', function () {
return view('example3');
});
// resources/views/example3.blade.php
The last part of the route URI is <b>{{ array_slice(explode('/', request()->route()->getPath()), -1, 1) }}</b>.
回答by Ruffles
Try request()->segment($number)
it should give you a segment of the URL.
试试request()->segment($number)
吧,它应该会给你一段 URL。
In your example, it should probably be request()->segment(2)
or request()->segment(3)
based on the number of segments the URL has.
在您的示例中,它可能应该是request()->segment(2)
或request()->segment(3)
基于 URL 的段数。
回答by Paul Basenko
It was useful for me:
这对我很有用:
request()->path()
from www.test.site/news
来自 www.test.site/news
get -> news
获取 -> 新闻
回答by Claudio King
Try with:
尝试:
{{ array_pop(explode('/',$_SERVER['REQUEST_URI'])) }}
It should work well.
它应该工作得很好。