如何在Laravel 5.6应用程序中突出显示当前活动菜单?

时间:2018-11-01 09:52:06

标签: laravel-5 twitter-bootstrap-3 menu

我在laravel应用程序中使用了以下引导菜单项,

<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<!------ Include the above in your HEAD tag ---------->

<div class="container">
    <div class="row">
        <div class="col-sm-3 col-md-3">
            <div class="panel-group" id="accordion">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h4 class="panel-title">
                            <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne"><span class="glyphicon glyphicon-folder-close">
                            </span> Content Management</a>
                        </h4>
                    </div>
                    <div id="collapseOne" class="panel-collapse collapse in">
                        <div class="panel-body">
                            <table class="table">
                                <tr>
                                    <td>
                                        <span class="glyphicon glyphicon-pencil text-primary"></span><a href="{{route('categories.index')}}">Category</a>
                                    </td>
                                </tr>

                                <tr>
                                    <td>
                                        <span class="glyphicon glyphicon-pencil text-primary"></span><a href="{{route('brands.index')}}">Brand</a>
                                    </td>
                                </tr>

                                <tr>
                                    <td>
                                        <span class="glyphicon glyphicon-pencil text-primary"></span><a href="{{route('models.index')}}">Model</a>
                                    </td>
                                </tr>

                                <tr>
                                    <td>
                                        <span class="glyphicon glyphicon-flash text-success"></span><a href="{{route('provinces.index')}}">Province</a>
                                    </td>
                                </tr>

                            </table>
                        </div>
                    </div>
                </div>
</div>

,并将此菜单栏与@include('menubar)一起用于其他刀片文件。现在,当我访问每个页面时,我需要突出显示当前菜单项。我该怎么办?

2 个答案:

答案 0 :(得分:0)

使用

{{ request()->route()->getName() }}

{{ route()->currentRouteName() }}

然后您可以做类似的事情

<a class="{{ request()->route()->getName() === "brands.index" ? "active" : "not-active" }}">

答案 1 :(得分:0)

您可以使用我编写的此软件包来执行此操作:https://packagist.org/packages/arcesilas/active-state

代码示例:

href="../others/page2.html"

此软件包还允许您检查url,具有给定参数的路由或查询。