featurit/featurit-sdk-laravel
Composer 安装命令:
composer require featurit/featurit-sdk-laravel
包简介
Laravel SDK for FeaturIT
关键字:
README 文档
README
Laravel wrapper of the PHP client for the FeaturIT Feature Flag management platform.
Description
This package aims to simplify the integration of the FeaturIT API in a Laravel project.
Getting started
Dependencies
- PHP >= 8.0.2
- laravel/framework >= 5.1
- psr/http-client-implementation
- psr/simple-cache-implementation
Installing
composer require featurit/featurit-sdk-laravel
If there's no package providing psr/http-client-implementation, visit https://packagist.org/providers/psr/http-client-implementation and choose the package that better suits your project.
If there's no package providing psr/simple-cache-implementation, visit https://packagist.org/providers/psr/simple-cache-implementation and choose the package that better suits your project.
Inside your config/app.php file, in the providers array add:
/*
* Package Service Providers...
*/
Featurit\Client\Laravel\FeaturitServiceProvider::class,
If you want to publish the default configuration file in order to customize things like the default FeaturitUserContextProvider, use the following command:
php artisan vendor:publish --provider="Featurit\Client\Laravel\FeaturitServiceProvider"
Basic Usage
That's how you would use Featurit in one of your controllers, services, or anywhere inside your PHP codebase:
if (Featurit::isActive('YOUR_FEATURE_NAME')) {
your_feature_code();
}
Or in order to check which is the version of your feature:
if (Featurit::version('YOUR_FEATURE_NAME') == 'v1') {
your_feature_code_for_v1();
} else if (Featurit::version('YOUR_FEATURE_NAME') == 'v2') {
your_feature_code_for_v2();
}
Blade directives
For convenience we provide 3 blade directives which allow to load blade components depending on the Feature Flag values.
Inside your blade template, you can use them like this:
<div>
<h2>This code will always be visible</h2>
@ifFeatureIsActive('MY_ACTIVE_FEATURE')
<h2>This will be visible</h2>
@endifFeatureIsActive
@ifFeatureIsNotActive('MY_ACTIVE_FEATURE')
<h2>This will NOT be visible</h2>
@endifFeatureIsNotActive
@ifFeatureVersionEquals('FEATURE_WITH_VERSIONS', 'v1')
<h2>Welcome to v1!</h2>
@endifFeatureVersionEquals
@ifFeatureVersionEquals('FEATURE_WITH_VERSIONS', 'v2')
<h2>Welcome to v2!</h2>
@endifFeatureVersionEquals
</div>
Defining your FeaturitUserContext
In order to show different versions of a feature to different users, Featurit needs to know about the attributes your user has in a certain context.
You can define the context using the as follows:
$contextData = get_your_user_context_data();
Featurit::setUserContext(
new DefaultFeaturitUserContext(
$contextData['userId'],
$contextData['sessionId'],
$contextData['ipAddress'],
[
'role' => $contextData['role'],
...
]
)
);
Defining a custom FeaturitUserContextProvider
This is an alternative to using Featurit::setUserContext(...);.
By default, Featurit SDK for Laravel comes with a default FeaturitUserContextProvider in your config/featurit.php file
'featurit_user_context_provider' => Featurit\Client\Laravel\Providers\LaravelFeaturitUserContextProvider::class,
But you can create your own implementation in order to add custom attributes so they can be used in the segmentation process.
Let's say that your platform users have a "role" attribute that you use to decide which features you show to each user. In that case you could create an implementation like:
<?php
namespace My\Namespace\Of\Choice;
use Featurit\Client\Modules\Segmentation\DefaultFeaturitUserContext;
use Featurit\Client\Modules\Segmentation\FeaturitUserContext;
use Featurit\Client\Modules\Segmentation\FeaturitUserContextProvider;
use Illuminate\Support\Facades\Auth;
class MyCustomFeaturitUserContextProvider implements FeaturitUserContextProvider
{
public function getUserContext(): FeaturitUserContext
{
if (! Auth::check()) {
return new DefaultFeaturitUserContext(
null,
null,
null,
[
'role' => 'Guest',
]
);
}
$user = Auth::user();
$userId = $user->getAuthIdentifier();
$sessionId = session()->getId();
$ipAddress = request()->ip();
$role = $user->role;
return new DefaultFeaturitUserContext(
$userId,
$sessionId,
$ipAddress,
[
'role' => $role,
]
);
}
}
Then you must replace your implementation in the config/featurit.php file
'featurit_user_context_provider' => My\Namespace\Of\Choice\MyCustomFeaturitUserContextProvider::class,
And that should do it, from now on your segmentation rules will use the role attribute.
Event Tracking
In order to track some event in your application, you can add this once the event has happened:
Featurit::track('EVENT_NAME', [
'some_property_name' => 'some_property_value',
'another_property_name' => 'another_property_value',
...
]);
In order to attack some person profile with your events, you can do so by calling the trackPerson() method before tracking any event.
Featurit::trackPerson();
All the events and people you track in the same request will be accumulated and associated to the current FeaturitUserContext, if for some reason you want to send the event immediately, you can do as follows:
Featurit::flush();
Authors
FeaturIT
featurit/featurit-sdk-laravel 适用场景与选型建议
featurit/featurit-sdk-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 120 次下载、GitHub Stars 达 3, 最近一次更新时间为 2022 年 12 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「feature」 「laravel」 「feature flag」 「feature toggle」 「a b testing」 「user targeting」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 featurit/featurit-sdk-laravel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 featurit/featurit-sdk-laravel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 featurit/featurit-sdk-laravel 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A block that displays featured content - large image, title, description and link.
Cold template caches that can be flagged and automatically invalidated.
Laravel Eloquent boolean & timestamp flagged attributes behavior.
Bitwise flag setting for Yii 2.x
MaxAl Subscriptions is a flexible plans and subscription management system for Laravel, with the required tools to run your SAAS like services efficiently. It's simple architecture, accompanied by powerful underlying to afford solid platform for your business.
Country Flag Emoji for PHP
统计信息
- 总下载量: 120
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-12-06