cjmellor/blockade
Composer 安装命令:
composer require cjmellor/blockade
包简介
Allow a User Model to block another User Model
README 文档
README
Allow a User to block another User. Blocking a User will prevent the blocker from seeing the blockees data.
Installation
You can install the package via composer:
composer require cjmellor/blockade
You can publish and run the migrations with:
php artisan vendor:publish --tag="blockade-migrations"
php artisan migrate
and publish the config file with:
php artisan vendor:publish --tag="blockade-config"
This is the contents of the published config file:
return [ /** * If your User model different to the default, you can specify it here. */ 'user_model' => 'App\Models\User', /** * Specify the user model's foreign key. */ 'user_foreign_key' => 'user_id', /** * Specify the table name for the blocks table. */ 'blocks_table' => 'blocks', /** * Specify the foreign key for the blocker. */ 'blocker_foreign_key' => 'blocker_id', /** * Specify the foreign key for the blocked. */ 'blocked_foreign_key' => 'blocked_id', /** * Schedule the cleanup of expired blocks. */ 'schedule_cleanup' => false, ];
Usage
Using the Traits
CanBlock
Add the CanBlock trait to your User model.
This trait should only ever be added to your User model.
use Cjmellor\Blockade\Concerns\CanBlock; class User { use CanBlock; // ... }
HasBlocked
Use this trait in your Models where you would not want a User to see the Model of a User they have blocked.
Example: you have a
CommentModel and User 7 has blocked User 8 so User 7 should not be able to see any comments made by User 8.
use Cjmellor\Blockade\Concerns\HasBlocked; class Comment { use HasBlocked; // ... }
Block a User
A User can block another User by supplying a User model, or just the ID of the User to be blocked.
$user->block(User::find(2))); // or $user->block(2);
Note: You cannot block yourself.
Unblock a User
A User can unblock another User by supplying a User model, or just the ID of the User to be unblocked.
$user->unblock(User::find(2))); // or $user->unblock(2);
Checking if a User is blocked
You can run a check to see if you're already blocking a User or not.
$user->isBlocking(User::find(2))); // or $user->isBlocking(2);
Set a block expiry
You can set an expiry date when blocking a User. This will automatically unblock the User after the expiry date has passed.
$user->block(User::find(2), expiresAt: now()->addDays(7)));
This example will block the User for 7 days. After 7 days, the User will be unblocked.
⏱️ Scheduling the cleanup of expired blocks
By default, a blocked User with an expiry will not be removed after the expiry time as past.
If you want to automatically remove expired blocks, you can set the schedule_cleanup config value in the config/blockade config file to true.
If you want more control over when the cleanup is run, there is an artisan command that can be run to remove expired blocks.
php artisan unblock:expired
You can also schedule this command to run automatically.
$schedule->command('unblock:expired')->everyMinute();
Note
Don't forget to add the command to the
App\Console\Kernelschedule()method.
Events
When a User is blocked, a UserBlocked event is fired.
When a User is unblocked, a UserUnblocked event is fired.
The User Model of the blocked or unblocked User is passed to the event so you can listen for these events and perform further actions.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
License
The MIT License (MIT). Please see License File for more information.
cjmellor/blockade 适用场景与选型建议
cjmellor/blockade 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 20, 最近一次更新时间为 2023 年 03 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「cjmellor」 「blockade」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cjmellor/blockade 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cjmellor/blockade 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cjmellor/blockade 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This package allows users to gain experience points (XP) and progress through levels by performing actions on your site. It can provide a simple way to track user progress and implement gamification elements into your application
Approve or Deny new Model data before it is persisted
This package allows users to gain experience points (XP) and progress through levels by performing actions on your site. It can provide a simple way to track user progress and implement gamification elements into your application
This package allows users to gain experience points (XP) and progress through levels by performing actions on your site. It can provide a simple way to track user progress and implement gamification elements into your application
Blockade Labs SDK for Laravel
Alfabank REST API integration
统计信息
- 总下载量: 14
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 20
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-03-17