php
43 lines · 8 steps
How a Laravel registration endpoint works
A registration controller that creates a user atomically, fires events, and queues a delayed welcome email.
Explained by
highlit
1<?php
2
3namespace App\Http\Controllers\Auth;
4
5use App\Http\Controllers\Controller;
6use App\Http\Requests\RegisterRequest;
7use App\Jobs\SendWelcomeEmail;
8use App\Models\User;
9use Illuminate\Auth\Events\Registered;
10use Illuminate\Http\JsonResponse;
11use Illuminate\Support\Facades\DB;
12use Illuminate\Support\Facades\Hash;
13
14class RegisterController extends Controller
15{
16 public function store(RegisterRequest $request): JsonResponse
17 {
18 $user = DB::transaction(function () use ($request) {
19 $user = User::create([
20 'name' => $request->validated('name'),
21 'email' => $request->validated('email'),
22 'password' => Hash::make($request->validated('password')),
23 ]);
24
25 $user->profile()->create([
26 'locale' => $request->getPreferredLanguage(['en', 'fr', 'de']) ?? 'en',
27 ]);
28
29 return $user;
30 });
31
32 event(new Registered($user));
33
34 SendWelcomeEmail::dispatch($user)
35 ->onQueue('mail')
36 ->delay(now()->addMinutes(2));
37
38 return response()->json([
39 'message' => 'Registration successful.',
40 'user' => $user->only('id', 'name', 'email'),
41 ], 201);
42 }
43}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Wrapping related writes in a database transaction keeps your data consistent when one step fails.
- 2Hashing passwords and dispatching side effects like emails to a queue keeps the request fast and secure.
- 3Returning only the fields a client needs avoids leaking sensitive data in API responses.
Related explainers
go
package main import ( "errors"
Parsing and validating CLI flags in Go
cli-parsing
validation
error-handling
Intermediate
8 steps
javascript
'use server' import { revalidatePath } from 'next/cache' import { redirect } from 'next/navigation'
How a Next.js Server Action updates a post
server-actions
authorization
validation
Intermediate
7 steps
php
<?php namespace App\Support;
Locale-aware formatting with PHP's intl extension
internationalization
encapsulation
constructor-injection
Intermediate
7 steps
php
<?php namespace App\Support;
Merging query params onto a URL in PHP
url-parsing
query-strings
immutability
Intermediate
8 steps
php
<?php class ImageUploadService {
Validating file uploads safely in PHP
file-upload
input-validation
security
Intermediate
8 steps
php
<?php namespace App\View;
Building a safe HTML escaper in PHP
security
xss
escaping
Intermediate
6 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-a-laravel-registration-endpoint-works-explained-php-a2ef/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.