How to register a user in Laravel 5.3?

Published 1 year ago by g385700

It seems that user registration/login has been revamped in Laravel 5.3.

There are now RegisterController.php and LoginController.php classes in App\Http\Controllers\Auth.

However, I'm unsure of how to utilize these classes.

I set up the following route for my Registration form:

Route::post('register', 'Auth\RegisterController');

But I get the error:

Invalid route action: [App\Http\Controllers\Auth\RegisterController]

Here is what RegisterController.php looks like (nothing changed, it's the base class):

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'username' => 'required|max:20',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'username' => $data['username'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }
}

What am I doing wrong?

Snapey
Snapey
1 year ago (652,995 XP)

Why not just use the helper Route::auth();?

If you want to vary it, at least include it once, do a artisan route:list, see what it added and then roll your own.

toniperic
toniperic
1 year ago (220,135 XP)

@g385700 you get that error because you haven't specified action (method name), thus the error "Invalid route action".

Instead of

Route::post('register', 'Auth\RegisterController');

you should do

Route::post('register', 'Auth\[email protected]');

Route::auth() does this for you, plus registers some more routes. You can see here.

g385700

@Snapey @toniperic

Another question. If the form has an error, it returns JSON like this:

{"username":["The username may only contain letters and numbers."],"email":["The email field is required."],"password":["The password field is required."]}

Is there a way that I can modify the JSON so that it's more like:

{"errors":["username":["The username may only contain letters and numbers."],"email":["The email field is required."],"password":["The password field is required."]]}
Snapey
Snapey
1 year ago (652,995 XP)

Surely you could just wrap it client side?

Fawwad

How to send mail after user register

vipin93

use Mailable event

thekavish

There is a list of route methods that Auth::routes(); adds which can be seen here

Please sign in or create an account to participate in this conversation.