使用 Blade 在 Laravel 5 中获取当前 URL 的最后一部分
Get last part of current URL in Laravel 5 using Blade
如何在没有/
符号的情况下动态获取当前URL的最后一部分?
例如:
在www.news.com/foo/bar
中得到bar
。
在www.news.com/foo/bar/fun
中得到fun
。
在当前视图中将函数放在哪里或如何实现?
试试 request()->segment($number)
它应该给你一段 URL。
在您的示例中,根据 URL 具有的段数,它可能应该是 request()->segment(2)
或 request()->segment(3)
。
试试:
{{ array_pop(explode('/',$_SERVER['REQUEST_URI'])) }}
它应该很好用。
Route object 是您想要的信息来源。您可以通过几种方式获取信息,其中大部分涉及将某些内容传递给您的视图。我强烈建议不要在 blade 内进行工作,因为这是控制器操作的目的。
正在将值传递给 blade
最简单的方法是将路由的最后一部分作为参数并将该值传递给视图。
// 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>.
避免路由参数需要更多的工作。
// 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>.
使用 request helper.
在 blade 中完成所有操作
// 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>.
当然总有Laravel方式:
request()->segment(count(request()->segments()))
对我有用:
request()->path()
来自 www.test.site/news
获取 -> 新闻
我是这样做的:
{{ collect(request()->segments())->last() }}
使用 basename()
和 Request::path()
。
basename(request()->path())
您应该能够从代码中的任何位置调用它,因为 request()
是 Laravel 中的全局辅助函数,而 basename()
是标准的 PHP 函数,它也可在全球范围内使用。
您可以使用Laravel的辅助函数last
。像这样:
last(request()->segments())
你的控制者:
use Illuminate\Support\Facades\URL;
file.blade.php:
echo basename(URL::current());
我也有同样的问题。与此同时Laravel 8。我总结了所有我知道的可能性。
您可以在您的网络路由中进行测试:
http(s)://127.0.0.1:8000/bar/foo ||巴兹
http(s)://127.0.0.1:8000/bar/bar1/foo ||巴兹
Route::get('/foo/{lastPart}', function(\Illuminate\Http\Request $request, $lastPart) {
dd(
[
'q' => request()->segment(count(request()->segments())),
'b' => collect(request()->segments())->last(),
'c' => basename(request()->path()),
'd' => substr( strrchr(request()->path(), '/'), 1),
'e' => $lastPart,
]
)->where('lastPart', 'foo,baz'); // the condition is only to limit
我更喜欢变体 e)。
正如@Qevo 已经在他的回答中写的那样。您只需将最后一部分作为请求的一部分。要缩小范围,您可以在路线中放置 WHERE 条件。
如何在没有/
符号的情况下动态获取当前URL的最后一部分?
例如:
在www.news.com/foo/bar
中得到bar
。
在www.news.com/foo/bar/fun
中得到fun
。
在当前视图中将函数放在哪里或如何实现?
试试 request()->segment($number)
它应该给你一段 URL。
在您的示例中,根据 URL 具有的段数,它可能应该是 request()->segment(2)
或 request()->segment(3)
。
试试:
{{ array_pop(explode('/',$_SERVER['REQUEST_URI'])) }}
它应该很好用。
Route object 是您想要的信息来源。您可以通过几种方式获取信息,其中大部分涉及将某些内容传递给您的视图。我强烈建议不要在 blade 内进行工作,因为这是控制器操作的目的。
正在将值传递给 blade
最简单的方法是将路由的最后一部分作为参数并将该值传递给视图。
// 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>.
避免路由参数需要更多的工作。
// 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>.
使用 request helper.
在 blade 中完成所有操作// 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>.
当然总有Laravel方式:
request()->segment(count(request()->segments()))
对我有用:
request()->path()
来自 www.test.site/news
获取 -> 新闻
我是这样做的:
{{ collect(request()->segments())->last() }}
使用 basename()
和 Request::path()
。
basename(request()->path())
您应该能够从代码中的任何位置调用它,因为 request()
是 Laravel 中的全局辅助函数,而 basename()
是标准的 PHP 函数,它也可在全球范围内使用。
您可以使用Laravel的辅助函数last
。像这样:
last(request()->segments())
你的控制者:
use Illuminate\Support\Facades\URL;
file.blade.php:
echo basename(URL::current());
我也有同样的问题。与此同时Laravel 8。我总结了所有我知道的可能性。
您可以在您的网络路由中进行测试:
http(s)://127.0.0.1:8000/bar/foo ||巴兹
http(s)://127.0.0.1:8000/bar/bar1/foo ||巴兹
Route::get('/foo/{lastPart}', function(\Illuminate\Http\Request $request, $lastPart) {
dd(
[
'q' => request()->segment(count(request()->segments())),
'b' => collect(request()->segments())->last(),
'c' => basename(request()->path()),
'd' => substr( strrchr(request()->path(), '/'), 1),
'e' => $lastPart,
]
)->where('lastPart', 'foo,baz'); // the condition is only to limit
我更喜欢变体 e)。
正如@Qevo 已经在他的回答中写的那样。您只需将最后一部分作为请求的一部分。要缩小范围,您可以在路线中放置 WHERE 条件。