Laravel Notes

Laravel Notes

5 features that can be customised in Laravel registration

5 features that can be customised in Laravel registration

Setting up registration is pretty simple in Laravel. With the artisan auth command you can get the logic, ui and migrations for Laravel registration. This registration page will be a generalised registration page with common features seen in most websites.

When we spin up a registration for our client, there will always be requirements to customise registration. Like for some websites, we may need to use username instead of email or in some cases we may need to redirect to a specific path after registration.

So let's look at five ways to customize laravel registration to satisfy our particular requirements.

1) Redirect after registration - The default behaviour of Laravel registration is to redirect to the home page with route “/home”. This redirect path can be customised from RegisterController.php. Refer the following code.

protected function redirectTo()
{
    if (auth()->user()->role_id == 1) {
        return '/admin';
    }
}

Or you can just assign the new path to redirectTo property in RegisterController.

class RegisterController extends Controller
{
    protected $redirectTo = '/home';

2) Change validation rule in laravel registration form - Validation rule for password field in normal laravel registration form is just string and minimum 6 characters. I recently came across a requirement to implement a complex validation rule for password. So I created a validator method in RegisterController to override the default validator and added my new validation rules.

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:6', 'confirmed', [New Rules]],
    ]);
}

3) New fields in laravel registration form - A laravel registration form usually has fields for name, email and password. Sometimes there will be requirements to add additional fields like age in the laravel registration form. This can be done by the following code updates in default registration code:

Let's assume we need an age field in registration form. First we need to edit registration blade from path [resources/views/auth/register.blade.php] and add following code in the form like this:

<div class="form-group row">
            <label for="name" class="col-md-4 col-form-label text-md-right">
              {{ __('Age') }}
            </label>
            <div class="col-md-6">
                <input id="age" type="number"
                class="form-control{{ $errors->has('age') ? ' is-invalid' : '' }}"
                name="age" value="{{ old('age') }}" required autofocus>
                @if ($errors->has('age'))
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $errors->first('age') }}</strong>
                    </span>
                @endif
            </div>
</div>

Then we add the new age field in database migration file of users table and update the create method in RegisterController

protected $fillable = [
    'name', 'email', 'age', 'password',
];

protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'age' => $data['age'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);
}

4) Enable/Disable registration options.

Auth::routes() method provides multiple options to customise laravel registration. These options can be passed as parameters to routes method.

4.1) Enable Email Verification - If your application have a requirement to have email verification, you can pass param like this:

Auth::routes(['verify' => true]);

And add verified middleware to the route

Route::get('profile', function () {
    // Only verified users may enter...
})->middleware('verified');

4.2) Disable Reset Password - To remove reset password feature from your registration form, pass

Auth::routes(['reset' => false]);

4.3) Disable registration

Auth::routes(['register' => false]);

5) Replace Email with Username in laravel user login.

Email and password are the normal fields present in most login forms. But there are some cases where clients prefer a username instead of email. A username can be more friendly for a user I guess. In this case, we can add a username field in migrations and update Logincontroller like this:

class LoginController extends Controller
{
    use AuthenticatesUsers;
    // ... All other code

    public function username()
    {
        return 'username';
    }
}

Conclusion

What makes Laravel the best PHP framework ? I think Laravel is the most developer friendly framework. It is highly customisable. Every feature in the Laravel framework can be tweaked to match our requirements.

#laravel#auth#laravel-5
 
Share this