Performance: MVC architecture in Laravel
This affects how quickly the server can prepare and send the HTML to the browser, impacting initial page load speed and responsiveness.
<?php // Controller fetches data only class UserController extends Controller { public function index() { $users = User::all(); return view('users.index', compact('users')); } } // Blade template handles HTML rendering {{-- resources/views/users/index.blade.php --}} @foreach ($users as $user) <p>{{ $user->name }}</p> @endforeach
<?php // Controller handling database and view logic together class UserController extends Controller { public function index() { $users = DB::table('users')->get(); foreach ($users as $user) { echo "<p>" . $user->name . "</p>"; } } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Controller mixes DB and HTML output | N/A (server-side) | N/A | N/A | [X] Bad |
| Controller fetches data, Blade renders HTML | N/A (server-side) | N/A | N/A | [OK] Good |
show method of UserController in Laravel?Route::get('uri', 'Controller@method') format for controller methods./welcome route?public function welcome() {
$name = 'Alice';
return view('greeting', ['name' => $name]);
}greeting.blade.php view contains:<h1>Hello, {{ $name }}!</h1>public function index() {
$users = User::all;
return view('users.index', compact('users'));
}Pass the test and this pattern moves unknown