Laravel Blade: 如何获得 parent URL
Laravel Blade: How to get the parent URL
有没有办法在blade中检索当前URL的parentURL?例如,如果我在 /users/1
,我想到达 /users
。例如:
<a href="{{ parent() }}">Back</a>
我希望它可重复用于所有资源,所以我不想要 url('/users')
。
如果总是
< something >/number
你可以使用request()->segment(1);
如果不是这种情况,您可以编写一个可重用的帮助程序
public function removeLastSegment(string $url): string
{
$stringParts = explode('/', $url);
array_pop($stringParts); // remove last element
return implode('/', $stringParts);
}
这是我的解决方案:
@php $url = url()->current(); @endphp
<a href="{{ substr($url,0,strrpos($url,'/')) }}">Back</a>
有没有办法在blade中检索当前URL的parentURL?例如,如果我在 /users/1
,我想到达 /users
。例如:
<a href="{{ parent() }}">Back</a>
我希望它可重复用于所有资源,所以我不想要 url('/users')
。
如果总是
< something >/number
你可以使用request()->segment(1);
如果不是这种情况,您可以编写一个可重用的帮助程序
public function removeLastSegment(string $url): string
{
$stringParts = explode('/', $url);
array_pop($stringParts); // remove last element
return implode('/', $stringParts);
}
这是我的解决方案:
@php $url = url()->current(); @endphp
<a href="{{ substr($url,0,strrpos($url,'/')) }}">Back</a>