承接 huddlebv/laravel-unleash 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

huddlebv/laravel-unleash

Composer 安装命令:

composer require huddlebv/laravel-unleash

包简介

An Unleash client for Laravel

README 文档

README

A simple Unleash component for Laravel. It is compatible with the Unlesah-hosted.com SaaS offering, Unleash Open-Source, and GitLab's Feature Flag system, which is built on Unleash.

V2 of this package is a wrapper and extension of the Unleash PHP SDK that is compatible with Laravel.

Getting started

1. Install the Laravel Unleash via Composer

composer require j-webb/laravel-unleash

2. Configure

Create local configuration (optional)

php artisan vendor:publish --provider="JWebb\Unleash\Providers\ServiceProvider"

Required .env values

# Your Unleash instance endpoint
UNLEASH_URL=https://app.unleash-hosted.com/

Optional .env values

# Enable or disable the Laravel Unleash client. If disabled, all feature checks will return false
UNLEASH_ENABLED=true

# For compatibility with Unleash V4, or other authentcation methods. Appends itself to the `Authorization` header for each request
UNLEASH_API_KEY=123456

# Instance id for this application (typically hostname, podId or similar)
UNLEASH_INSTANCE_ID=default 

# The Unleash environment name, which can be used to as a parameter for enabling/disabling features for local or development environments
# See: https://docs.getunleash.io/advanced/strategy_constraints#constrain-on-a-specific-environment
UNLEASH_ENVIRONMENT=production 

# Automatically registers the client instance with the unleash server
UNLEASH_AUTOMATIC_REGISTRATION=true 

# Enable/Disable metrics
UNLEASH_METRICS=true 

# Enable/Disable failsafe cache for data
# See: https://docs.getunleash.io/client-specification#system-overview
UNLEASH_CACHE_ENABLED=true 
UNLEASH_CACHE_TTL=30 

Setting up the Middleware

The module comes bundled with middleware for you to perform a feature check on routes and/or controllers.

#/app/Http/Kernel.php
protected $routeMiddleware = [
    ...
    'feature' => \JWebb\Unleash\Middleware\CheckFeature::class,
    ...
];

Once added to your Kernel.php file, you can use this in any area where middleware is applicable. As an example, you could use this in a controller.

public function __construct()
{
    $this->middleware('feature:your_feature_name');
}

See the Laravel Docs for more information.

Setting up a custom cache handler

If the cache option is enabled, by default the component will use the Laravel Cache module. By utilizing the UnleashCacheHandlerInterface, you can create your own PSR-16 compatible implementation and override the unleash.cache.handler config value with your handler class.

Setting up custom strategies

To add your own strategy, you can override or create your own Strategies Provider. A UnleashStrategiesProviderInterface is included for convenience. Once your custom class is build, you should modify the unleash.strategy_provider config value.

Overwriting default context provider

If you want to send more context by default, you can overwrite the UnleashContextProvider. Make sure that your class implements the Unleash\Client\ContextProvider\UnleashContextProvider interface (to prevent confusion with the UnleashContextProvider in this package, an option would be to alias it). After that, change the config value of unleash.context_provider to your custom created class.

Usage

Checking individual features

if (Unleash::isEnabled('your_feature')) {
    // Your feature is enabled
}

Using array of features.

// List of all features, enabled or disabled
$allFeatures = Unleash::getFeatures();
Result: [
    'toggles' => [
        'feature_1' => [
            'enabled' => true,
            'name' => 'feature_1'
        ],
        'feature_2' => [
            'enabled' => false,
            'name' => 'feature_2'
        ]
    ]
]

// List of all enabled features
$enabledFeatures = Unleash::getFeatures(true);
Result: [
    'toggles' => [
        'feature_1' => [
            'enabled' => true,
            'name' => 'feature_1'
        ]
    ]
]

Note: The result matches that which would be returned from the official Unleash Proxy instances. This means you could use your Laravel application as an Unleash Proxy endpoint, which is compatible with the official client-side Unleash Proxy SDKs

Using middleware on a controller

class ExampleController extends Controller
{
    public function __construct()
    {
        ...
        $this->middleware('feature:your_feature');
    }
}

Using middleware on a route

Route::get('/', function () {
    //
})->middleware('feature:your_feature');

Because the component is a wrapper of the official Unleash Client SDK, you can pass relevant context to your checks:

$context = (new UnleashContext())
    ->setCurrentUserId('some-user-id-from-app')
    ->setIpAddress('127.0.0.1')
    ->setSessionId('sess-123456');
$enabled = Unleash::isEnabled('some-feature', $context);

Note: User ID information is automatically added to the context using the Laravel Auth module

Or get variant information

$variant = $unleash->getVariant('nonexistentFeature');
assert($variant->isEnabled() === false);

Blade

You can use the Unleash Blade directive for checking if a feature is enabled in your Blade templates:

@featureEnabled('betaTester')
    <p>You are a beta tester</p>
@endfeatureEnabled

Or if a feature is disabled:

@featureDisabled('betaTester')
    <p>Enroll now to be a beta tester.</p>
@endfeatureDisabled

huddlebv/laravel-unleash 适用场景与选型建议

huddlebv/laravel-unleash 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 110.85k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 02 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 huddlebv/laravel-unleash 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-07