承接 mateusjunges/laravel-invite-codes 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

mateusjunges/laravel-invite-codes

Composer 安装命令:

composer require mateusjunges/laravel-invite-codes

包简介

This package allows you to easily manage invite codes for your Laravel application.

README 文档

README

Readme banner This package allows you to easily manage invite codes for your Laravel application.

Total Downloads Latest Stable Version License Continuous integration

Sponsor my work!

If you think this package helped you in any way, you can sponsor me on GitHub!

Sponsor Me

Documentation

Installation

To get started with Laravel Invite Codes, use Composer to add the package to your project's dependencies:

composer require mateusjunges/laravel-invite-codes

Or add this line in your composer.json, inside the require section:

{
    "require": {
        "mateusjunges/laravel-invite-codes": "^2.3",
    }
}

then run composer install.

All migrations required for this package are already included. If you need to customize the tables, you can publish them with:

php artisan vendor:publish --provider="Junges\InviteCodes\InviteCodesServiceProvider" --tag="invite-codes-migrations"

and set the config for custom_migrations to true, which is false by default.

'custom_migrations' => true,

After the migrations have been published you can create the tables on your database by running the migrations:

php artisan migrate

If you change the table names on migrations, please publish the config file and update the tables array. You can publish the config file with:

php artisan vendor:publish --provider="Junges\InviteCodes\InviteCodesServiceProvider" --tag="invite-codes-config"

When published, the config/invite-codes.php config file contains:

<?php

return [
    /*
    |--------------------------------------------------------------------------
    |  Models
    |--------------------------------------------------------------------------
    |
    | When using this package, we need to know which Eloquent Model should be used
    | to retrieve your invites. Of course, it is just the basics models
    | needed, but you can use whatever you like.
    |
    */
    'models' => [
        'invite_model' => \Junges\InviteCodes\Models\Invite::class,
    ],

    /*
    |--------------------------------------------------------------------------
    | Tables
    |--------------------------------------------------------------------------
    | Specify the basics authentication tables that you are using.
    | Once you required this package, the following tables are
    | created by default when you run the command
    |
    | php artisan migrate
    |
    | If you want to change this tables, please keep the basic structure unchanged.
    |
    */
    'tables' => [
        'invites_table' => 'invites',
    ],

    /*
    |--------------------------------------------------------------------------
    | User
    |--------------------------------------------------------------------------
    | To use the ProtectedByInviteCode middleware provided by this package, you need to
    | specify the email column you use in the model you use for authentication.
    | If not specified, only invite code with no use restrictions can be used in this middleware.
    |
    */
    'user' => [
        'email_column' => 'email',
    ],

    /*
    |--------------------------------------------------------------------------
    | Custom migrations
    |--------------------------------------------------------------------------
    | If you want to publish this package migrations and edit with new custom columns, change it to true.
    */
    'custom_migrations' => false,
];

Usage

This package provides a middleware called ProtectedByInviteCodeMiddleware. If you want to use it to protect your routes, you need to register it in your $routeMiddleware array, into app/Http/Kernel.php file:

$routeMiddleware = [
    'protected_by_invite_codes' => ProtectedByInviteCodeMiddleware::class,
];

Now you can protect your routes using middleware rules:

Route::get('some-route', function() {
    //
})->middleware('protected_by_invite_codes');

You can also add it to the __construct(), in your controllers:

public function __construct()
{
    $this->middleware('protected_by_invite_codes');
}

Note This middleware expects to find an invitation code within the invite_code key of your request.

Creating invite codes

To create a new invite code, you must use the InviteCodes facade. Here is a simple example:

$invite_code = \Junges\InviteCodes\Facades\InviteCodes::create()
    ->expiresAt('2020-02-01')
    ->maxUsages(10)
    ->restrictUsageTo('contato@mateusjunges.com')
    ->save();

The code above will create a new invite code, which can be used 10 times only by a logged in user who has the specified email contato@mateusjunges.com.

The methods you can use with the InviteCodes facade are listed below:

Set the expiration date of your invite code

To set the expiration date of your invite code you can use one of the following methods:

  • expiresAt(): This method accept a date string in yyyy-mm-dd format or a Carbon instance, and set the expiration date to the specified date.
  • expiresIn(): This method accept an integer, and set the expiration date to now plus the specified amount of days.

Restrict usage to some specific user:

To restrict the usage of an invite code you can use the restrictUsageTo() method, and pass in the email of the user who will be able to use this invite code.

Set the maximum allowed usages for an invite code:

If you want that your invite code be used a limited amount of times, you can set the max usages limit with the maxUsages() method, and pass an integer with the amount of allowed usages.

Create multiple invite codes

If you want to create more than one invite code with the same configs, you can use the make() method. This method generates the specified amount of invite codes. For example:

\Junges\InviteCodes\Facades\InviteCodes::create()
    ->maxUsages(10)
    ->expiresIn(30)
    ->make(10);

The code above will create 10 new invite codes which can be used 10 times each, and will expire in 30 days from now.

Redeeming invite codes

To redeem an invite code, you can use the redeem method:

\Junges\InviteCodes\Facades\InviteCodes::redeem('YOUR-INVITE-CODE');

When any invite is redeemed, the InviteRedeemedEvent will be dispatched.

Redeeming invite codes without dispatching events

If you want to redeem an invite codes without dispatch the InviteRedeemedEvent, you can use the withoutEvents() method:

\Junges\InviteCodes\Facades\InviteCodes::withoutEvents()->redeem('YOUR-INVITE-CODE');

Customizing how invite codes are generated

By default, this package generates a random 16 characters string that. Sometimes, you may want to customize how your invitation code is generated, for adding a prefix to the invitation code or anything you need.

If you need to customize how your invite codes are generated, you can add a call to the InviteCodes facade createInviteCodesusing method, in your service provider:

\Junges\InviteCodes\Facades\InviteCodes::createInviteCodeUsing(static function () {
    return 'THIS-IS-MY-INVITE-'.\Illuminate\Support\Str::random(); 
});

From now on, all of your invites will have the THIS-IS-MY-INVITE- prefix.

Also, the package itself will handle duplicate invites, so you don't need to take care of that yourself.

Extending the Invite model

The \Junges\InviteCodes\Models\Invite is fully extendable and replaceable. You can extend or create a new model to be used instead of the default one, and the only thing you need to do is implement the \Junges\InviteCodes\Contracts\InviteContract interface, which contains some required methods for this package to work.

After implementing the contract, you need to change the models.invite_model configuration value in config/invite-codes.php.

Handling invite codes exceptions

If you want to override the default 403 response, you can catch the exceptions using the laravel exception handler:

public function render($request, Exception $exception)
{
    if ($exception instanceof \Junges\InviteCodes\Exceptions\InviteWithRestrictedUsageException) {
        //
    }
    if ($exception instanceof \Junges\InviteCodes\Exceptions\ExpiredInviteCodeException) {
        //
    }
    if ($exception instanceof \Junges\InviteCodes\Exceptions\DuplicateInviteCodeException) {
        //
    }
    if ($exception instanceof \Junges\InviteCodes\Exceptions\InvalidInviteCodeException) {
        //
    }
    if ($exception instanceof \Junges\InviteCodes\Exceptions\UserLoggedOutException) {
        //
    }
    if ($exception instanceof \Junges\InviteCodes\Exceptions\InviteMustBeAbleToBeRedeemedException) {
        //
    }
    if ($exception instanceof \Junges\InviteCodes\Exceptions\SoldOutException) {
        //
    }
    if ($exception instanceof \Junges\InviteCodes\Exceptions\RouteProtectedByInviteCodeException) {
        //
    }
    
    return parent::render($request, $exception);
}

Using artisan commands

This package also provides a command to delete all expired invites from your database. You can use it like this:

\Illuminate\Support\Facades\Artisan::call('invite-codes:clear');

After all expired invites has been deleted, it will dispatch the DeletedExpiredInvitesEvent.

Tests

Run composer test to test this package.

Contributing

Thank you for considering contributing for the Laravel Invite Codes package! The contribution guide can be found here.

Changelog

Please see changelog for more information about the changes on this package.

License

The Laravel Invite Codes package is open-sourced software licenced under the MIT License. Please see the License File for more information.

mateusjunges/laravel-invite-codes 适用场景与选型建议

mateusjunges/laravel-invite-codes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 84.61k 次下载、GitHub Stars 达 290, 最近一次更新时间为 2020 年 02 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 mateusjunges/laravel-invite-codes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mateusjunges/laravel-invite-codes 我们能提供哪些服务?
定制开发 / 二次开发

基于 mateusjunges/laravel-invite-codes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 84.61k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 291
  • 点击次数: 25
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 290
  • Watchers: 5
  • Forks: 14
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-02-15