sebastienheyd/active 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sebastienheyd/active

Composer 安装命令:

composer require sebastienheyd/active

包简介

Helper class for Laravel to get the active class based on the current route

README 文档

README

tests Laravel Packagist Nb downloads License

Helper class for Laravel to get the active class based on the current url.

This project is based on hieu-le/active

Installation

Require this package as your dependencies:

composer require sebastienheyd/active

Configuration (optional)

You can define another default class name instead of active by editing the active.php configuration file.

To publish the configuration file you can use the following command:

php artisan vendor:publish --tag=active

Usage

  • Use the alias: Active::getClassIf($condition, $activeClass = 'active', $inactiveClass = '')
  • Use the application container: app('active')->getClassIf($condition, $activeClass = 'active', $inactiveClass = '')
  • Use the helper function: active_class($condition, $activeClass = 'active', $inactiveClass = '')

Explanation: if $condition is true, the value of $activeClass is returned, otherwise the value of $inactiveClass is returned.

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'

Check the current URI

All of checking methods return boolean result (true or false). You can use the result in the condition of active_class or write your own expression.

Check the whole URI

Usage:

  • Use the alias: Active::checkUri(array $uris)
  • Use the application container: app('active')->checkUri(array $uris)
  • Use the helper function: if_uri(array $uris)

Explanation: you give an array of URI, the package will return true if the current URI is in your array. Remember that an URI does not begin with the slash (/) except the root.

Check the URI with some patterns

Usage:

  • Use the alias: Active::checkUriPattern(array $patterns)
  • Use the application container: app('active')->checkUriPattern(array $patterns)
  • Use the helper function: if_uri_pattern(array $patterns)

Explanation: you give an array of patterns, the package will return true if the current URI matches one of the given pattern. Asterisks may be used in the patterns to indicate wildcards.

Check the query string

Usage:

  • Use the alias: Active::checkQuery($key, $value)
  • Use the application container: app('active')->checkQuery($key, $value)
  • Use the helper function: if_query($key, $value)

Explanation: the package will return true if one of the following condition is true:

  • The current query string contains a parameter named $key with any value and the value of $value is false.
  • The current query string does not contain a parameter named $key and the value of $value is null.
  • The current query string contains a parameter named $key whose value is a string equals to $value.
  • The current query string contains a parameter named $key whose value is an array that contain the $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

Check the current route

Check the exact route name

Usage:

  • Use the alias: Active::checkRoute(array $routes)
  • Use the application container: app('active')->checkRoute(array $routes)
  • Use the helper function: if_route(array $routes)

Explanation: you give an array of route names, the package will return true if the name of the current route (which can be null) is in your array.

Check the route name with some patterns

Usage:

  • Use the alias: Active::checkRoutePattern(array $patterns)
  • Use the application container: app('active')->checkRoutePattern(array $patterns)
  • Use the helper function: if_route_pattern(array $patterns)

Explanation: you give an array of patterns, the package will return true if the name of the current route (which can be null) matches one of the given pattern. Asterisks may be used in the patterns to indicate wildcards.

Check the route parameter value

Usage:

  • Use the alias: Active::checkRouteParam($key, $value)
  • Use the application container: app('active')->checkRouteParam($key, $value)
  • Use the helper function: if_route_param($key, $value)

Explanation: the package will return true if one of the following condition is true:

  • The current route contains a parameter named $key whose value is $value.
  • The current route does not contain a parameter named $key and the value of $value is null.

Read more about route parameter in the Laravel documentation.

Get the current values

Get the current action

Usage:

  • Use the alias: Active::getAction()
  • Use the application container: app('active')->getAction()
  • Use the helper function: current_action()

Explanation: if the current route is bound to a class method, the result will be a string like App\Http\Controllers\YourController@yourMethod. If the route is bound to a closure, the result will be the Closure string.

Get the current controller class

Usage:

  • Use the alias: Active::getController()
  • Use the application container: app('active')->getController()
  • Use the helper function: current_controller()

Explanation: if the current route is bound to a class method, the result will be the full qualified class name of the controller class, like App\Http\Controllers\YourController. If the route is bound to a closure, the result will be the Closure string.

Get the current controller method

Usage:

  • Use the alias: Active::getMethod()
  • Use the application container: app('active')->getMethod()
  • Use the helper function: current_method()

Explanation: if the current route is bound to a class method, the result will be the name of the controller method. like yourMethod. If the route is bound to a closure, the result will be the empty string.

Example

The example below illustrate the usage of this package in a sidebar with Bootstrap list group :

<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>

sebastienheyd/active 适用场景与选型建议

sebastienheyd/active 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16.78k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 03 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「routing」 「laravel」 「active」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 sebastienheyd/active 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sebastienheyd/active 我们能提供哪些服务?
定制开发 / 二次开发

基于 sebastienheyd/active 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 16.78k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 14
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-22