Do More During Registration in Laravel 5

by Colin DeCarlo on September 9, 2015

Laravel 5 ships with some really handy traits that will take care of some of the more mundane aspects of building a modern web app. The RegistersUsers trait used by the AuthController handles functionality which I was happy to hand off to the framework. For those unfamiliar with it, the RegistersUsers trait handles loading your registration view and processing the registration form submission (this includes validation and logging in your newly registered user).

The trait itself isn’t completely plug’n’play however as you do need to create your own registration view in resources/views/auth/register.blade.php for it to render. The validator and create methods created in your AuthController are expecting specific (yet fairly standard) form parameters so pay close attention when naming your inputs on the registration form.

Often times however you’ll find that more is expected from your registration process by your web app’s stakeholders. At the very least, you’ll typically need to send a welcome email to your new user. You may also need to start an email drip to keep them engaged with your app or even need to feed the registration event into an analytics tool. Whatever the case may be, you’ll quickly find that the RegistersUsers trait falls short of being able to supply this extended functionality and it might not be immediately clear as to how to implement it.

Copying the postRegister method from RegistersUsers and tacking your custom code onto the end before the return may be your first inclination but that doesn’t seem right (and would probably leave you feeling like a bad person). If you reflect a little on your gut instinct however what you’re saying is “I want exactly what it was doing just a little more“, this is the Decorator pattern. Since the postRegister method comes from a trait, decorating it is a little more involved than decorating a method implemented in a parent class but it’s not impossible. Thanks to PHP’s ability to alias trait methods on import you can simply bring in postRegister as something else; freeing up postRegister to be implemented in your own AuthController.

Here is a complete example:

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illumninate\Foundation\Auth\ThrottlesLogins;
use Illumninate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{

    use AuthenticatesAndRegistersUsers, ThrottlesLogins {
        AuthenticatesAndRegistersUsers::postRegister as register;
    }

    // other controller methods elided for brevity

    public function postRegister(Request $request)
    {
        // call the RegistersUsers::postRegister method
        // hold onto the return value for later
        $redirect = $this->register($request);

        $user = $request->user();

        Mail::send('emails.welcome', ['user' => $user], function ($m) use ($user) {
            $m->to($user->email, $user->name)->subject('Welcome to AwesomeApp!');
        });

        return $redirect;
    }
}

As you can see, adding more functionality to your registration process in this way is pretty straight forward and you’re still able to let the framework do the heavy lifting. Everybody wins.

The following two tabs change content below.
LAMP developer, the web app kind not the fra-ghe-ley kind from Italy. Hobbyist cook and photographer. I don't discriminate when it comes to hacking.

Latest posts by Colin DeCarlo (see all)