Skip to content
Send a welcome notification to new users
PHP HTML
Branch: master
Clone or download
Latest commit ccf8b6a Nov 21, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github wip Nov 12, 2019
database/migrations replace token based links with signed links Nov 14, 2019
resources/views wip Nov 14, 2019
src Expiration computed from validUntil Nov 20, 2019
tests wip Nov 15, 2019
.editorconfig wip Nov 12, 2019
.gitattributes wip Nov 12, 2019
.gitignore
.scrutinizer.yml wip Nov 12, 2019
.styleci.yml wip Nov 12, 2019
.travis.yml wip Nov 12, 2019
CHANGELOG.md Update CHANGELOG.md Nov 20, 2019
CONTRIBUTING.md wip Nov 12, 2019
LICENSE.md wip Nov 12, 2019
README.md
composer.json
phpunit.xml.dist wip Nov 12, 2019

README.md

Send a welcome notification to new users

Latest Version on Packagist Build Status Quality Score StyleCI Total Downloads

Using this package you can send a WelcomeNotification to a new user of your app. The notification contains a secure link to a screen where the user can set an initial password.

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

$user->sendWelcomeNotification($expiresAt);

Installation

You can install the package via composer:

composer require spatie/laravel-welcome-notification

Migrating the database

You must publish the migrations provided by this package by executing this command:

php artisan vendor:publish --provider="Spatie\WelcomeNotification\WelcomeNotificationServiceProvider" --tag="migrations"

Next, you must migrate your database.

php artisan migrate

Preparing the user model

You must apply the \Spatie\WelcomeNotification\ReceivesWelcomeNotification trait to your User model.

Preparing the WelcomeController

Next you'll need to create a controller of your own that will extend Spatie\WelcomeNotification\WelcomeController. This controller will be used to show the welcome form and to save the password set by a user.

namespace App\Http\Controllers\Auth;

use Spatie\WelcomeNotification\WelcomeController as BaseWelcomeController;

class MyWelcomeController extends BaseWelcomeController
{
}

Registering the routes

You'll have to register these routes:

use Spatie\WelcomeNotification\WelcomesNewUsers;
use App\Http\Controllers\Auth\MyWelcomeController;

Route::group(['middleware' => ['web', WelcomesNewUsers::class,]], function () {
    Route::get('welcome/{user}', [MyWelcomeController::class, 'showWelcomeForm'])->name('welcome');
    Route::post('welcome/{user}', [MyWelcomeController::class, 'savePassword']);
});

Preparing the welcome form view

The welcome view that ships with the package, will be rendered when somebody clicks the welcome link in the welcome notification mail. You should style this view yourself. You can publish the views with this command:

php artisan vendor:publish --provider="Spatie\WelcomeNotification\WelcomeNotificationServiceProvider" --tag="views"

Usage

Here's how you can send a welcome notification to a user that you just created.

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

$user->sendWelcomeNotification($expiresAt);

Handling successful requests

After the a user has successfully set a new password the sendPasswordSavedResponse of the WelcomeController will get called.

use Symfony\Component\HttpFoundation\Response;

class MyWelcomeController extends BaseWelcomeController
{
    public function sendPasswordSavedResponse(): Response

    {
        return redirect()->route('home');
    }
}

Customizing the notification

By default the WelcomeNotification will send a mail. If you wish to customize the mail you can extend WelcomeNotification and override the buildWelcomeNotificationMessage method.

class MyCustomWelcomeNotification extends WelcomeNotification
{
    public function buildWelcomeNotificationMessage(): Illuminate\Notifications\Messages\MailMessage
    {
        return (new MailMessage)
            ->subject('Welcome to my app')
            ->action(Lang::get('Set initial password'), $this->showWelcomeFormUrl)
    }
}

To use the custom notification you must add a method called sendWelcomeNotification to your User model.

public function sendWelcomeNotification(\Carbon\Carbon $validUntil)
{
    $this->notify(new MyCustomWelcomeNotification($validUntil));
}

Validating extra fields

The default welcome form that ships with this package only asks for a password. You can add more fields to the form by publishing the view and adding more fields to it.

To validate new fields you can override the rules function in your own WelcomeController. Here's an example where we want to validate an extra field named job_title.

class MyWelcomeController extends BaseWelcomeController
{
    public function rules()
    {
        return [
            'password' => 'required|confirmed|min:6',
            'job_title' => 'required',
        ];
    }
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.

Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

Support us

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.

License

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

You can’t perform that action at this time.