带有链接的 Laravel 动态面包屑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37966729/
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
Laravel dynamic breadcrumbs with links
提问by Wasif Iqbal
I am trying to implement dynamic breadcrumbs in laravel with links. I successfully render the breadcrumbs but without links by following code.
我正在尝试使用链接在 laravel 中实现动态面包屑。我通过以下代码成功渲染了面包屑但没有链接。
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i>Marketplace</a></li>
@foreach(Request::segments() as $segment)
<li>
<a href="#">{{$segment}}</a>
</li>
@endforeach
</ol>
But now i am facing issue with the urls. I am getting the current url of the route with all the decendents. Can someone please help me that how can I add links to the breadcrumbs ?
但是现在我遇到了网址问题。我正在获取所有后代的路线的当前网址。有人可以帮助我如何添加指向面包屑的链接吗?
Thanks.
谢谢。
回答by user1669496
If I understand your issue correctly, you just need to fill in the URL of the link. This is untested but I think it should work.
如果我正确理解您的问题,您只需填写链接的 URL。这是未经测试的,但我认为它应该有效。
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i>Marketplace</a></li>
<?php $segments = ''; ?>
@foreach(Request::segments() as $segment)
<?php $segments .= '/'.$segment; ?>
<li>
<a href="{{ $segments }}">{{$segment}}</a>
</li>
@endforeach
</ol>
回答by Eamon
Im not sure if you already got a solution for this, but i figured out a way to go about it on my project. It might come in handy for your implementation.
我不确定您是否已经为此找到了解决方案,但我想出了一种方法来解决我的项目。它可能对您的实施有用。
I ended up with either adding the whole url to the link or only the segment, which ofc is not desirable, so using array slice i start slicing from the 0 index in the array and only slice untill the current iteration of the loop, then implode the array into a string and then use URL::to to create the link.
我最终要么将整个 url 添加到链接中,要么只添加段,这不是我们想要的,所以使用数组切片我从数组中的 0 索引开始切片,并且只切片直到循环的当前迭代,然后内爆将数组转换为字符串,然后使用 URL::to 创建链接。
<ol class="breadcrumb">
<li>
<i class="fa fa-home"></i>
<a href="{{route('admin.index')}}">HOME</a>
</li>
@for($i = 2; $i <= count(Request::segments()); $i++)
<li>
<a href="{{ URL::to( implode( '/', array_slice(Request::segments(), 0 ,$i, true)))}}">
{{strtoupper(Request::segment($i))}}
</a>
</li>
@endfor
</ol>
As you will notice, I only start my iteration from 2 ($i = 2) as my app base url starts at /admin and i manually put my home Url in the first breadcrumb.
您会注意到,我只从 2 ($i = 2) 开始我的迭代,因为我的应用程序基本 url 从 /admin 开始,我手动将我的 home url 放在第一个面包屑中。
Again you might already have a solution, but throught this could work for people who do not want to add the package to get breadcrumbs.
同样,您可能已经有了一个解决方案,但是这对于不想添加包来获取面包屑的人来说可能有用。
回答by Rohan Khude
This worked for me, tried in Laravel 5.4.*
这对我有用,在 Laravel 5.4.* 中尝试过
Requirement for this code to work flawlessly: All URL's should have hierarchy pattern in your routes file
此代码完美运行的要求:所有 URL 都应在您的路由文件中具有层次结构模式
Below code will create crumb for each path -
下面的代码将为每个路径创建面包屑 -
<a href="/">Home</a> >
<?php $link = "" ?>
@for($i = 1; $i <= count(Request::segments()); $i++)
@if($i < count(Request::segments()) & $i > 0)
<?php $link .= "/" . Request::segment($i); ?>
<a href="<?= $link ?>">{{ ucwords(str_replace('-',' ',Request::segment($i)))}}</a> >
@else {{ucwords(str_replace('-',' ',Request::segment($i)))}}
@endif
@endfor
So Breadcrumb for URLyour_site.com/abc/lmn/xyz
will be - Home> abc> lmn> xyz
所以URL 的面包屑your_site.com/abc/lmn/xyz
将是 - Home> abc> lmn> xyz
Hope this helps!
希望这可以帮助!
回答by Mohamed Laabid
Just add slash /
before any link to add decendents to domain name
like that
只需/
在任何链接前添加斜杠即可将后代添加到域名中
<a href="/YourLink" ></a>
<a href="/YourLink" ></a>