lucadello91/laravel-active

此包已被弃用,不再维护。未建议替代包。

Laravel 助手,用于检测活跃导航菜单项并应用 Bootstrap 类。

2.0 2017-08-08 19:21 UTC

This package is not auto-updated.

Last update: 2020-01-22 19:23:17 UTC


README

Software License Packagist Version Total Downloads

Laravel 助手,用于检测活跃导航菜单项并应用 Bootstrap 类。这是 https://github.com/laravelista/Ekko 的实现。在此实现中,您还可以使用资源名称。

安装

首先,通过 Composer 拉取包。

"require": {
    "lucadello91/laravel-active": "^2.0"
}

然后,如果您使用 Laravel 5 或 4,请在 app/config/app.php 中包含服务提供者。

'providers' => [
    Lucadello91\Active\LaravelActiveServiceProvider::class
];

为了方便起见,在文件底部添加一个外观别名

'aliases' => [
    'Active'    => Lucadello91\Active\Facades\LaravelActive::class
];

使用

您可能会在 navbar 部分中使用此包,如下所示

<li>
    <a href="{{ route('home') }}" class="{{ Active::isActiveRoute('home') }}">
        <i class="halflings white home"></i> Home
    </a>
</li>

<li>
    <a href="#" class="{{ Active::areActiveRoutes(['murter', 'kornati']) }}">
        <i class="halflings white screenshot"></i> Location
    </a>
    <ul>
        <li>
            <a href="{{ route('murter') }}">Murter</a>
        </li>
        <li>
            <a href="{{ route('kornati') }}">Kornati</a>
        </li>
    </ul>
</li>

<li>
    <a href="{{ route('trips.index') }}" class="{{ Active::isActiveMatch('trips') }}">
        <i class="halflings white road"></i> Trips
    </a>
</li>

API

作为任何方法的第二个参数,您可以传递如果匹配要返回的值。默认值为 active,这是 Bootstrap 默认值。

作为任何方法的第三个参数,您可以传递如果未匹配要返回的值。默认为空字符串。

isActiveRoute($routeName, $active = "active", $notActive = "")

比较给定的路由名称与当前路由名称。

{{ Active::isActiveRoute('home') }}

isActiveRouteResourceName($resourceName, $active = "active", $notActive = "")

比较给定的资源名称与当前 Route::Resource 名称。

{{ Active::isActiveRouteResourceName('home') }}

isActiveURL($url, $active = "active", $notActive = "")

比较给定的 URL 与当前 URL。

{{ Active::isActiveURL('/about') }}

isActiveMatch($string, $active = "active", $notActive = "")

检测给定的字符串是否出现在当前 URL 中。

{{ Active::isActiveMatch('bout') }}

areActiveRoutes(array $routeNames, $active = "active", $notActive = "")

比较给定的路由名称数组与当前路由名称。

{{ Active::areActiveRoutes(['product.index', 'product.show']) }}

areActiveRoutesResourcesNames(array $resourcesNames, $active = "active", $notActive = "")

比较给定的资源名称数组与当前 Route::Resource 名称。

{{ Active::areActiveRoutesResourcesNames(['post', 'comments']) }}

areActiveURLs(array $urls, $active = "active", $notActive = "")

比较给定的 URL 数组与当前 URL。

{{ Active::areActiveURLs(['/product', '/product/create']) }}