Laravel的辅助类,根据当前路由获取活动类

1.1.0 2024-03-26 09:07 UTC

This package is auto-updated.

Last update: 2024-09-26 10:18:19 UTC


README

Travis Laravel License

Laravel的辅助类,根据当前URL获取活动类。

本项目基于hieu-le/active

安装

将此包作为依赖项要求

composer require sebastienheyd/active

配置(可选)

您可以通过编辑active.php配置文件来定义另一个默认类名,而不是使用active

要发布配置文件,您可以使用以下命令

php artisan vendor:publish --tag=active

用法

  • 使用别名:Active::getClassIf($condition, $activeClass = 'active', $inactiveClass = '')
  • 使用应用程序容器:app('active')->getClassIf($condition, $activeClass = 'active', $inactiveClass = '')
  • 使用辅助函数:active_class($condition, $activeClass = 'active', $inactiveClass = '')

说明:如果$condition为真,则返回$activeClass的值,否则返回$inactiveClass的值。

active_class(true); // 'active' (default value set in the configuration file)
active_class(false); // ''
active_class(if_uri([$currentUri]), 'selected'); // 'selected'
active_class(if_uri_pattern([$pattern1, $pattern2]), 'active', 'other'); // 'other'

检查当前URI

所有检查方法都返回布尔值(truefalse)。您可以将结果用于active_class的条件中或编写自己的表达式。

检查整个URI

用法

  • 使用别名:Active::checkUri(array $uris)
  • 使用应用程序容器:app('active')->checkUri(array $uris)
  • 使用辅助函数:if_uri(array $uris)

说明:您提供一个URI数组,如果当前URI在您的数组中,则包将返回true。请记住,除了根之外,URI不以斜杠(/)开头。

检查带有一些模式的URI

用法

  • 使用别名:Active::checkUriPattern(array $patterns)
  • 使用应用程序容器:app('active')->checkUriPattern(array $patterns)
  • 使用辅助函数:if_uri_pattern(array $patterns)

说明:您提供一个模式数组,如果当前URI与提供的模式之一匹配,则包将返回true。模式中可以使用星号(*)表示通配符。

检查查询字符串

用法

  • 使用别名:Active::checkQuery($key, $value)
  • 使用应用程序容器:app('active')->checkQuery($key, $value)
  • 使用辅助函数:if_query($key, $value)

说明:如果以下任一条件为真,则包将返回true

  • 当前查询字符串包含名为$key的参数,无论值如何,并且$value的值为false
  • 当前查询字符串不包含名为$key的参数,并且$value的值为null
  • 当前查询字符串包含名为$key的参数,其值等于$value的字符串。
  • 当前查询字符串包含名为$key的参数,其值是一个包含$value的数组。
// the current query string is ?x=1&y[0]=a&y[1]=b

if_query('x', null); // true
if_query('x', 1); // true
if_query('x', 2); // false
if_query('y', 'a'); // true
if_query('y', 'c'); // false
if_query('z', null); // false

检查当前路由

检查精确路由名称

用法

  • 使用别名:Active::checkRoute(array $routes)
  • 使用应用程序容器:app('active')->checkRoute(array $routes)
  • 使用辅助函数:if_route(array $routes)

说明:您提供一个路由名称数组,如果当前路由的名称(可以是null)在您的数组中,则包将返回true。

检查路由名称是否与某些模式匹配

用法

  • 使用别名: Active::checkRoutePattern(array $patterns)
  • 使用应用容器: app('active')->checkRoutePattern(array $patterns)
  • 使用辅助函数: if_route_pattern(array $patterns)

说明:您提供一个模式数组,如果当前路由名称(可能为null)与给定的模式之一匹配,则包将返回true。模式中可以使用星号表示通配符。

检查路由参数值

用法

  • 使用别名: Active::checkRouteParam($key, $value)
  • 使用应用容器: app('active')->checkRouteParam($key, $value)
  • 使用辅助函数: if_route_param($key, $value)

说明:如果以下条件之一为真,则包将返回 true

  • 当前路由包含一个名为 $key 的参数,其值为 $value
  • 当前路由不包含名为 $key 的参数,且 $value 的值为 null。

有关路由参数的更多信息,请参阅 Laravel 文档

获取当前值

获取当前动作

用法

  • 使用别名: Active::getAction()
  • 使用应用容器: app('active')->getAction()
  • 使用辅助函数: current_action()

说明:如果当前路由绑定到类方法,结果将是一个类似 App\Http\Controllers\YourController@yourMethod 的字符串。如果路由绑定到闭包,则结果将是 Closure 字符串。

获取当前控制器类

用法

  • 使用别名: Active::getController()
  • 使用应用容器: app('active')->getController()
  • 使用辅助函数: current_controller()

说明:如果当前路由绑定到类方法,则结果将是控制器类的完全限定名称,如 App\Http\Controllers\YourController。如果路由绑定到闭包,则结果将是 Closure 字符串。

获取当前控制器方法

用法

  • 使用别名: Active::getMethod()
  • 使用应用容器: app('active')->getMethod()
  • 使用辅助函数: current_method()

说明:如果当前路由绑定到类方法,则结果将是控制器方法的名称,如 yourMethod。如果路由绑定到闭包,则结果将是空字符串。

示例

以下示例展示了在侧边栏中使用此包的方法,侧边栏采用 Bootstrap 列表组

<ul class="list-group">
    <a href="" class="list-group-item {{ active_class(if_route('users.list') && if_query('active', 1)) }}">
        Active users
    </a>
    <a href="#" class="list-group-item {{ active_class(if_route('users.list') && if_query('active', 0)) }}">
        Inactive users
    </a>
    <a href="#" class="list-group-item  {{ active_class(if_action('App\Http\Controllers\UserController@getNewUser')) }}">
        Add users
    </a>
</div>