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
php
<?php class NameParser {
Parsing a full name into components in PHP
string-parsing
arrays
normalization
Intermediate
8 steps
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
php
<?php namespace App\Services;
How a password strength validator works in PHP
validation
regular-expressions
data-driven
Intermediate
8 steps
php
<?php namespace App\Services;
Building a cached daily leaderboard in Laravel
caching
aggregation
eager-loading
Intermediate
9 steps
rust
use chrono::{Duration, NaiveDate}; #[derive(Debug)] pub struct DateRange {
Parsing and iterating date ranges in Rust
error-handling
iterators
parsing
Intermediate
7 steps
php
<?php final class RotatingFileLogger {
How a rotating file logger works in PHP
logging
file-rotation
io
Intermediate
9 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.