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
typescript
import { NestFactory } from '@nestjs/core'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import { ValidationPipe } from '@nestjs/common'; import { ApiProperty } from '@nestjs/swagger';
Wiring validation and Swagger docs in NestJS
validation
openapi
decorators
Intermediate
8 steps
python
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, status from pydantic import BaseModel, EmailStr from sqlalchemy.orm import Session
Building a signup endpoint in FastAPI
dependency-injection
request-validation
background-tasks
Intermediate
8 steps
php
<?php namespace App\Console\Commands;
Releasing stale document locks in Laravel
artisan-command
transactions
row-locking
Intermediate
6 steps
php
<?php namespace App\Providers;
Subdomain multi-tenancy routing in Laravel
multi-tenancy
service-container
route-binding
Advanced
7 steps
php
<?php namespace App\Broadcasting;
Authorizing presence channels in Laravel
broadcasting
authorization
presence-channels
Intermediate
3 steps
java
@RestController @RequestMapping("/api/products") public class ProductSearchController {
Binding collection query params in Spring
rest-api
query-parameters
dependency-injection
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.