PHP code example of offload-project / laravel-hoist
1. Go to this page and download the library: Download offload-project/laravel-hoist library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
declare(strict_types=1);
namespace App\Features;
use OffloadProject\Hoist\Attributes\Description;
use OffloadProject\Hoist\Attributes\FeatureSet;
use OffloadProject\Hoist\Attributes\Label;
use OffloadProject\Hoist\Attributes\Route;
use OffloadProject\Hoist\Attributes\Tags;
use OffloadProject\Hoist\Contracts\Feature;
#[Label('Billing Module')]
#[Description('Advanced billing features')]
#[Route('billing.index')]
#[Tags('subscription', 'pro')]
#[FeatureSet('premium')]
class BillingFeature implements Feature
{
public string $name = 'billing';
public function resolve(mixed $scope): mixed
{
return $scope->subscription?->isActive() ?? false;
}
public function metadata(): array
{
return [
'category' => 'premium',
'icon' => 'credit-card',
'version' => '2.0',
];
}
}
use OffloadProject\Hoist\Facades\Hoist;
// Get all features
$features = Hoist::all();
// Get features for a specific user with active status
$userFeatures = Hoist::forModel($user);
// Get array of all feature names
$featureNames = Hoist::names();
// Returns: ['billing', 'dashboard', 'reporting', ...]
// Access feature data
foreach ($userFeatures as $feature) {
echo $feature->name; // 'billing'
echo $feature->label; // 'Billing Module'
echo $feature->description; // 'Advanced billing features'
echo $feature->href; // route('billing.index')
echo $feature->active; // true/false
print_r($feature->metadata); // ['category' => 'premium', ...]
print_r($feature->tags); // ['subscription', 'pro']
}
// Get features with a specific tag
$flags = Hoist::tagged('flag');
$subscriptionFeatures = Hoist::tagged('subscription');
// Get features with ALL specified tags (AND logic)
$proSubscriptions = Hoist::withTags(['subscription', 'pro']);
// Get features with ANY of the specified tags (OR logic)
$paidFeatures = Hoist::withAnyTags(['pro', 'enterprise']);
// Filter with model scope (
// Properties-only approach (still supported)
class MyFeature implements Feature
{
public string $name = 'my-feature';
public string $label = 'My Feature';
public ?string $description = 'A description';
public ?string $route = 'my-feature.index';
public array $tags = ['flag'];
public string $featureSet = 'core';
public function resolve(mixed $scope): mixed
{
return true;
}
}
class FeatureData
{
public string $name; // Feature identifier
public string $label; // Human-readable name
public ?string $description; // Feature description
public ?string $href; // Generated route URL
public ?bool $active; // Active status (when using forModel)
public array $metadata; // Custom metadata
public array $tags; // Feature tags for categorization
public ?string $featureSet; // Feature set grouping
}
use Laravel\Pennant\Feature;
// Standard Pennant usage
if (Feature::active('billing')) {
// Feature is active
}
// In Blade
@feature('billing')
<!-- Feature content -->
@endfeature
// Combined with Hoist
$features = Hoist::forModel($user);
foreach ($features as $feature) {
if ($feature->active) {
// Do something with active feature
}
}
public function featureDashboard(Request $request)
{
$features = Hoist::forModel($request->user());
return view('features.dashboard', [
'features' => $features,
]);
}
Route::get('/api/features', function (Request $request) {
return Hoist::forModel($request->user());
});