定制 kurozora/laravel-cooldown 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

kurozora/laravel-cooldown

Composer 安装命令:

composer require kurozora/laravel-cooldown

包简介

Easily implement global or model-specific cooldowns into your Laravel app.

README 文档

README

Latest version on packagist GitHub Tests Action Status Quality score Total downloads Kurozora Open Source

plug-and-play global and model-specific cooldowns

Laravel Cooldowns

This Laravel package makes it easier to implement cooldowns into your app.
Consider the following example:

// The user will be able to post again 5 minutes from now
$user->cooldown('create-post')->for('5 minutes');

Installation

You can install the package via composer:

composer require kurozora/laravel-cooldown

Usage

Global cooldowns

Global cooldowns aren't tied to any model and are the same throughout your entire app.
Use the cooldown helper to create one:

cooldown('registration')->for('1 hour');

Here's an example of how you could limit registration to once per hour:

if(cooldown('registration')->notPassed())
    return 'Registration is currently unavailable.';

// ... perform account registration ...

cooldown('registration')->for('1 hour');

Model-specific cooldowns

Of course, a more useful use-case would be to tie cooldowns to models. In order to make use of this, you'll need to add the trait to your model:

use Illuminate\Database\Eloquent\Model;
use Kurozora\Cooldown\HasCooldowns;

class User extends Model
{
    use HasCooldowns;
}

The API used to interact with model-specific cooldowns is the exact same as global cooldowns, however you use the cooldown method on the model itself:

if($user->cooldown('create-post')->notPassed())
    return 'You cannot create a post right now.';

// ... create the post ...

$user->cooldown('create-post')->for('5 minutes');

All cooldown methods

These methods are available for both global and model-specific cooldowns.

for() Cooldown for a timespan
Pass along a string with the desired timespan.

cooldown('create-post')->for('1 day 3 hours');

until() Cooldown for a given datetime
Pass along a Carbon object with the desired datetime.

$tomorrow = now()->addDay();

cooldown('create-post')->until($tomorrow);

reset() Reset the cooldown
The cooldown will be reset, and the action will be available immediately.

cooldown('create-post')->reset();

passed()
Checks whether the cooldown has passed. Returns true if the cooldown hasn't ever been initiated.

cooldown('create-post')->passed(); // true/false

notPassed()
Checks whether the cooldown is still active, and thus hasn't passed yet.

cooldown('create-post')->notPassed(); // true/false

expiresAt() Get the expiration date
Returns the datetime at which the cooldown will pass.

cooldown('create-post')->expiresAt(); // Illuminate\Support\Carbon object

get()
Returns the underlying Cooldown model.

cooldown('create-post')->get(); // Kurozora\Cooldown\Models\Cooldown object

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email kurozoraapp@gmail.com instead of using the issue tracker.

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Musa

💻 📖

Kirito

🤔 🎨

This project follows the all-contributors specification. Contributions of any kind welcome!

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

  • Stars: 20
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固