Inviteable, inspired from Laravel Auth Invitations, but in this package, not for Auth, but for anything. Yes, we mean anything! Invitation to group, to class room, to meeting. Can be anything!
- In order to install
cleaniquecoders/inviteable
in your Laravel project, just run the composer require command from your terminal:
$ composer require cleaniquecoders/inviteable
- Then in your
config/app.php
add the following to the providers array:
CleaniqueCoders\Inviteable\InviteableServiceProvider::class,
- Run the migration file:
$ php artisan migrate
Inviteable provide a trait \CleaniqueCoders\Inviteable\Traits\HasInviteable
.
Following are the sample usage.
use CleaniqueCoders\Inviteable\Traits\HasInviteable;
class User extends Authenticatable
{
use HasInviteable;
}
$invitation = User::create([
'email' => '[email protected]',
'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),
]);
Once you have create the invitation, you may use the invitation with events and notifications.
Will add dispatching event on invitation created, so you can extend the use of the invitation to something else like notification.
More sample usage using 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.');
- On Invitation Created -
\CleaniqueCoders\Inviteable\Events\InvitationAccepted
- On Invitation Accepted -
\CleaniqueCoders\Inviteable\Events\InvitationAlreadyAccepted
- On Invitation Already Accepted -
\CleaniqueCoders\Inviteable\Events\InvitationCreated
Added Listener to send out e-mail invitation:
CleaniqueCoders\Inviteable\Listeners\Invitations
- You need to configure in yourapp/Providers/EventServiceProvider
to have this in your app.
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
'\CleaniqueCoders\Inviteable\Events\InvitationCreated' => [
'\CleaniqueCoders\Inviteable\Listeners\Invitations\SendInvitationEmail',
],
];
Added Middleware to be use to check only active invite able to get through
'inviteable' => \CleaniqueCoders\Inviteable\Http\Middleware\Inviteable::class,
Added config/inviteable.php
to handle redirection - using route name:
<?php
return [
'redirect' => [
'accepted_token' => 'invitation.index',
'already_accepted_token' => 'invitation.index',
'middleware' => 'invitation.access_denied'
],
];
Default route php artisan route:list --name=invitation
consist of
- Activation invitation - on success, you will redirect to
inviteable.redirect.accepted_token
route. You may overwrite this. In this route also handle already accepted invitation. Do specifyinviteable.redirect.already_accepted_token
route name to redirect to other page. - Access denied route - You can change the redirect by specify route name in
config.redirect.middleware
Run php artisan vendor:publish --tag=inviteable
to publish configuration and views for Inviteable.
To run the test, type vendor/bin/phpunit
in your terminal.
To have codes coverage, please ensure to install PHP XDebug then run the following command:
$ vendor/bin/phpunit -v --coverage-text --colors=never --stderr
Everyone are welcome to contribute to this package. However, it's a good practice to provide:
- The problem you solved
- Provide test
- Documentation
Without these 3, you may add extra work for the maintainer.
This package is open-sourced software licensed under the MIT license.