0
0
Laravelframework~8 mins

Why controllers organize request handling in Laravel - Performance Evidence

Choose your learning style9 modes available
Performance: Why controllers organize request handling
MEDIUM IMPACT
This affects how efficiently the server processes requests and how quickly responses are generated, impacting server response time and user experience.
Handling HTTP requests in a Laravel app
Laravel
<?php
// app/Http/Controllers/UserController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserController extends Controller {
    public function index() {
        $users = DB::table('users')->get();
        return view('users.index', ['users' => $users]);
    }

    public function store(Request $request) {
        DB::table('users')->insert(['name' => $request->name]);
        return redirect('/users');
    }
}

// routes/web.php
use App\Http\Controllers\UserController;

Route::get('/users', [UserController::class, 'index']);
Route::post('/users', [UserController::class, 'store']);
Centralizes request handling in controllers, reducing code duplication and improving maintainability and server response efficiency.
📈 Performance GainReduces server processing time by avoiding repeated logic and enables better caching and optimization.
Handling HTTP requests in a Laravel app
Laravel
<?php
// routes/web.php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

Route::get('/users', function () {
    $users = DB::table('users')->get();
    return view('users.index', ['users' => $users]);
});

Route::post('/users', function (Request $request) {
    DB::table('users')->insert(['name' => $request->name]);
    return redirect('/users');
});
Defining request logic directly in routes causes duplication and mixes routing with business logic, making maintenance harder and slower.
📉 Performance CostIncreases server processing time due to duplicated code and harder optimization; no caching benefits from centralized logic.
Performance Comparison
PatternCode DuplicationServer ProcessingMaintainabilityVerdict
Inline route logicHigh duplicationSlower due to repeated codeHard to maintain[X] Bad
Controller-based handlingLow duplicationFaster with centralized logicEasy to maintain[OK] Good
Rendering Pipeline
When a request arrives, Laravel routes it to a controller method that handles the logic and returns a response. Controllers separate routing from business logic, streamlining request processing.
Routing
Controller Execution
Response Generation
⚠️ BottleneckController Execution stage can be slow if logic is duplicated or scattered.
Core Web Vital Affected
INP
This affects how efficiently the server processes requests and how quickly responses are generated, impacting server response time and user experience.
Optimization Tips
1Use controllers to centralize request handling logic.
2Avoid placing business logic directly in route definitions.
3Centralized logic improves server response time and maintainability.
Performance Quiz - 3 Questions
Test your performance knowledge
Why does using controllers improve Laravel app performance?
ABecause controllers centralize logic reducing duplicated code and speeding up request handling
BBecause controllers automatically cache all responses
CBecause controllers reduce the number of HTTP requests sent by the browser
DBecause controllers load all data before routing
DevTools: Network
How to check: Open DevTools, go to Network tab, reload page, and inspect server response times for requests.
What to look for: Look for lower server response times and consistent response patterns indicating efficient request handling.