cleaniquecoders/inviteable

此包已被废弃,不再维护。没有建议的替代包。

使用 Laravel Standalone Package Creator 构建

v2.0.0 2020-09-09 16:12 UTC

README

Build Status Latest Stable Version Total Downloads License

关于您的包

Inviteable,灵感来自 Laravel Auth Invitations,但在这个包中,不是用于认证,而是用于任何事物。是的,我们指的是任何事物!邀请加入群组、教室、会议。可以是任何事物!

安装

  1. 为了在您的 Laravel 项目中安装 cleaniquecoders/inviteable,只需从您的终端运行 composer require 命令。
$ composer require cleaniquecoders/inviteable
  1. 然后在您的 config/app.php 中将以下内容添加到 providers 数组中
CleaniqueCoders\Inviteable\InviteableServiceProvider::class,
  1. 运行迁移文件
$ php artisan migrate

用法

Inviteable 提供了一个 trait \CleaniqueCoders\Inviteable\Traits\HasInviteable

以下是一些示例用法。

设置

use CleaniqueCoders\Inviteable\Traits\HasInviteable;

class User extends Authenticatable 
{
	use HasInviteable;
}

创建邀请

$invitation = User::create([
    'email'    => 'test@test.com',
    'name'     => 'Test Name',
    'password' => bcrypt('secret'),
])
    ->invitations()
    ->create([
        'name'       => 'Invitation',
        'token'      => str_random(64),
        'invited_by' => 1,
        'is_expired' => false,
        'expired_at' => \Carbon\Carbon::now()->addHours(24),
    ]);

创建邀请后,您可以使用事件和通知来使用邀请。

在创建邀请时将触发事件,因此您可以将邀请的扩展用于其他事物,如通知。

使用 routes/console.php 的更多示例用法

use App\User;

Artisan::command('invite', function() {
    // create a user that will invite other person
    $invitor = factory(User::class)->create();
    
    // to invite who
    $to_invite = factory(User::class)->create();
    
    // login using invitor
    auth()->loginUsingId($invitor->id);

    // invite user to a class
    $to_invite->invitations()->create([
        'name'       => 'Live Coding Class',
        'token'      => str_random(64),
        'invited_by' => auth()->user()->id,
        'is_expired' => false,
        'expired_at' => \Carbon\Carbon::now()->addHours(24),
    ]);
})->describe('Inivite the fastest way via cli.');

事件和监听器

  1. 在创建邀请时 - \CleaniqueCoders\Inviteable\Events\InvitationAccepted
  2. 在邀请接受时 - \CleaniqueCoders\Inviteable\Events\InvitationAlreadyAccepted
  3. 在邀请已被接受时 - \CleaniqueCoders\Inviteable\Events\InvitationCreated

添加了监听器来发送电子邮件邀请

  1. CleaniqueCoders\Inviteable\Listeners\Invitations - 您需要在您的 app/Providers/EventServiceProvider 中进行配置,以便将其添加到您的应用程序中。
/**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        '\CleaniqueCoders\Inviteable\Events\InvitationCreated' => [
            '\CleaniqueCoders\Inviteable\Listeners\Invitations\SendInvitationEmail',
        ],
    ];

中间件

添加了中间件,仅检查活动的可邀请才能通过

'inviteable' => \CleaniqueCoders\Inviteable\Http\Middleware\Inviteable::class,

配置

添加了 config/inviteable.php 以处理重定向 - 使用路由名称

<?php 

return [
    'redirect' => [
        'accepted_token' => 'invitation.index',
        'already_accepted_token' => 'invitation.index',
        'middleware' => 'invitation.access_denied'
    ],
];

路由

默认路由 php artisan route:list --name=invitation 包含

  1. 激活邀请 - 在成功后,您将重定向到 inviteable.redirect.accepted_token 路由。您可以覆盖此路由。在此路由中也可以处理已接受的邀请。请指定 inviteable.redirect.already_accepted_token 路由名称以重定向到其他页面。
  2. 访问拒绝路由 - 您可以通过在 config.redirect.middleware 中指定路由名称来更改重定向。

视图

运行 php artisan vendor:publish --tag=inviteable 以发布 Inviteable 的配置和视图。

测试

要运行测试,请在您的终端中键入 vendor/bin/phpunit

要实现代码覆盖率,请确保安装PHP XDebug,然后运行以下命令

$ vendor/bin/phpunit -v --coverage-text --colors=never --stderr

贡献

欢迎每个人为这个包做出贡献。然而,提供以下内容是一个良好的实践:

  1. 你解决的问题
  2. 提供测试
  3. 文档

如果没有这三项,你可能会给维护者增加额外的工作。

许可证

这个包是开源软件,根据MIT许可证授权。