Creating a custom registration form for your Laravel application involves several steps. In this blog post, we will guide you through the process of building a secure and user-friendly registration page.
Step 1: Set up the Blade view
First, create a new Blade view file called register.blade.php in the resources/views/auth directory of your project. This file will contain the HTML structure for your custom registration form.
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Register') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('register') }}">
@csrf
<!-- Add your custom registration fields here -->
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
Step 2: Modify the authentication controller
Next, open the AuthController.php file located in the app/Http/Controllers/Auth directory. This controller handles user registration and authentication.
use App\Models\User;
use Illuminate\Support\Facades\Hash;
public function register(Request $request)
{
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
// For example, send a confirmation email to the user
return redirect()->route('login')->with('success', __('Registration successful. Please log in.'));
}
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
Step 3: Customize the registration validation rules
Open the validation/registration.blade.php file located in the resources/views/auth directory. This Blade view contains the default validation messages for the registration form.
<div class="row mb-3">
<label for="name" class="col-md-4 col-form-label text-md-end">{{ __('Name') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control @error('name') is-invalid @enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>
@error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<!-- Add your custom validation messages for the registration fields here -->
Step 4: Customize the registration form
Now, let’s modify the register.blade.php file to include your custom registration fields.
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Register') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('register') }}">
@csrf
<!-- Add your custom registration fields here -->
<div class="row mb-3">
<label for="name" class="col-md-4 col-form-label text-md-end">{{ __('Name') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control @error('name') is-invalid @enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>
@error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<!-- Add more fields as needed, following the same pattern -->
<div class="row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Register') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
Step 5: Handle the registration form submission
When the user submits the custom registration form, the register method in the AuthController.php file is called. This method validates the input data using the validation rules defined earlier.
protected function validator(array $data)
{
return Validator::make($data, [
// Add your custom validation rules here
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
Step 6: Store the registered user data
If the validation passes, the create method in the AuthController.php file is called. This method creates a new user record in the users table of your database using the provided input data.
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
Step 7: Redirect and display success message After successfully registering the user, you can redirect them to a login page or display a success message. In this example, we are redirecting the user to the login page with a success message.
if ($this->isCustomerLoggedIn() && $this->customerHasAddresses()) {
return redirect()->route('login')->with('success', __('Registration successful. Please log in.'));
}
Conclusion: This guide provides an overview of how to create a custom registration form for your Laravel application. By following these steps, you can add additional fields to the default registration form and customize it according to your project requirements.
Remember to replace the example code snippets with your own implementation and adapt them as needed.
If you encounter any issues or have further questions, feel free to explore our website at Laravel Company for more detailed tutorials and resources on Laravel development.