rellix/dismissibles-for-laravel

一个用于在服务器端轻松处理可消失、重复出现的对象(如弹出窗口/通知/模态框)可见性的 Laravel 包。

5.1.0 2024-05-07 18:47 UTC

This package is auto-updated.

Last update: 2024-09-07 19:35:13 UTC


README

Dismissibles for Laravel

这是一个 Laravel 包,可以轻松管理后端中重复出现、可消失的对象(如弹出窗口/通知/模态框)的可见性。此包不包含前端组件,因此与您能使用的任何前端都兼容。

📕 目录

✅ 这个包解决了什么问题?

假设您有一个弹出窗口,您希望每天向每个用户展示一周。用户可以将其关闭,并且在整个当天都不会再次显示,直到第二天。

此包处理有关(可消失的)弹出窗口当前是否应对当前用户可见的复杂逻辑。它基本上处理您可消失对象的可见性。它高度可定制,使其非常灵活,适用于多种场景。

因为它是服务器端的,我们可以轻松获取有关谁何时何地关闭了什么的统计信息。

📦 安装

  1. 在您的 Laravel 应用程序中引入此包
composer require rellix/dismissibles-for-laravel
  1. 运行迁移以创建数据库表
php artisan migrate

❓ 如何使用

1. 将接口和特性添加到任何模型中

use Rellix\Dismissibles\Contracts\Dismisser;
use Rellix\Dismissibles\Traits\HasDismissibles;

class User implements Dismisser
{
    use HasDismissibles;
    
    ...
}

2. 创建可消失(迁移)

use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\DB;

return new class () extends Migration {
    public function up(): void
    {
        DB::table('dismissibles')->insert([
            'name'         => 'Test Popup', // This is your **unique** identifier
            'active_from'  => Date::createFromFormat('d-m-Y', '01-03-2024'),
            'active_until' => null, // Optional end date
            'created_at'   => Date::now(),
            'updated_at'   => Date::now(),
        ]);
    }
};

并运行您的创建迁移

php artisan migrate
💡 您还可以使用 "active"-范围和 "firstOrCreate" 创建/检索一个内联可消失。
Dismissible::active()->firstOrCreate(
    ['name' => 'Test Popup'], 
    [
        'active_from'  => Date::createFromFormat('d-m-Y', '01-03-2024'),
        'active_until' => null,
        'created_at'   => Date::now(),
        'updated_at'   => Date::now(),
    ]
);

3. 检查它当前是否应可见

use Rellix\Dismissibles\Facades\Dismissibles;

$showPopup = Dismissibles::shouldBeVisible('Test Popup', $user);

// Here are some more examples, including ones with additional conditionals:
$showPopup = Dismissibles::shouldBeVisible('Happy New Year 2025 Popup', $user);
$showPopup = Dismissibles::shouldBeVisible('Newsletter signup modal', $user) && !$user->is_subscribed;
$showPopup = Dismissibles::shouldBeVisible('Complete your profile notification', $user) && !$user->has_completed_profile;
$showPopup = Dismissibles::shouldBeVisible('50% Off First Purchase Popup', $user) && !$user->has_orders;

// You can also get all Dismissibles in one query (performance) and use the model methods.
$dismissibles = Dismissibles::getAllFor($user);
💡 您还可以使用单个模型。
use Rellix\Dismissibles\Facades\Dismissibles;

$popup = Dismissibles::get('Test Popup');

$showPopup = $popup->shouldBeVisibleTo($user);

4. 为指定时间段关闭它

use Rellix\Dismissibles\Facades\Dismissibles;

Dismissibles::dismiss('Test Popup', $user)->untilNextWeek();

// Here's an overview of all the ways you can dismiss:
Dismissibles::dismiss('Test Popup', $user)
    ->untilTomorrow();
    ->untilNextWeek();
    ->untilNextMonth();
    ->untilNextQuarter();
    ->untilNextYear();
    ->until($dateTime);
    ->forHours($numberOfHours);
    ->forDays($numberOfDays);
    ->forWeeks($numberOfWeeks);
    ->forMonths($numberOfMonths);
    ->forYears($numberOfYears);
    ->forever();
💡 您还可以使用单个模型。
use Rellix\Dismissibles\Facades\Dismissibles;

$popup = Dismissibles::get('Test Popup');

// Here's an overview of all the ways you can dismiss:
$popup->dismissFor($user)
    ->untilTomorrow();
    ->untilNextWeek();
    ->untilNextMonth();
    ->untilNextQuarter();
    ->untilNextYear();
    ->until($dateTime);
    ->forHours($numberOfHours);
    ->forDays($numberOfDays);
    ->forWeeks($numberOfWeeks);
    ->forMonths($numberOfMonths);
    ->forYears($numberOfYears);
    ->forever();

❗ 需要注意的事情

  • 外观包含一些通过 $name 的单行代码,但您也可以根据需要使用 DismissibleDismissal Eloquent 模型中的范围/方法,以实现最大灵活性。
  • 建议将可消失名称集中在一个枚举(或配置)中
  • 需要有关关闭的额外数据?所有关闭方法都允许您传递一个作为最后一个参数的 $extraData 数组,它将被写入 dismissals 表作为 json。
  • 请随时要求更多的方法/范围

💾 数据库表

数据库结构允许您轻松跟踪有关可消失的活动。由于 extra_data 列,它也非常灵活!

可消失(弹出窗口、通知、模态框)

关闭(活动)

☕ 买我一杯咖啡

如果您喜欢这个包,请考虑买我一杯咖啡 :-).