In the previous lesson, we learned about routes and how they connect a URL to some logic.
But writing all your logic directly in routes/web.php quickly becomes messy.
That’s where Controllers come in — they help organize your code by keeping it in separate classes.
What is a Controller?
A Controller is a PHP class in Laravel that groups related request-handling logic together.
Instead of putting all your logic inside a route closure, you move it into a method inside a controller.
Without Controller (in routes/web.php):
Route::get('/about', function () {
return view('about');
});
With Controller:
use App\Http\Controllers\PageController;
Route::get('/about', [PageController::class, 'about']);
Creating a Controller
Laravel has an Artisan command to create controllers.
Run in your terminal:
php artisan make:controller PageController
This will create a file:
app/Http/Controllers/PageController.php
Writing a Method in the Controller
Open PageController.php and add your method:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PageController extends Controller
{
public function about()
{
return view('about');
}
}
Connecting Route to Controller
In routes/web.php:
use App\Http\Controllers\PageController;
Route::get('/about', [PageController::class, 'about']);
Now, when someone visits /about, Laravel will call the about() method in PageController.
Passing Data to Views
Controllers make it easy to pass data to views:
public function about()
{
$team = ['Ali', 'Sara', 'John'];
return view('about', ['team' => $team]);
}
In resources/views/about.blade.php:
<h1>About Us</h1>
<ul>
@foreach($team as $member)
<li>{{ $member }}</li>
@endforeach
</ul>
Types of Controllers
Laravel offers different controller types for different use cases:
- Basic Controller – Handles a few related pages.
- Resource Controller – Automatically creates all CRUD methods.
php artisan make:controller ProductController --resourceThis gives you methods likeindex,create,store,show,edit,update,destroy.
Benefits of Using Controllers
- Keeps your routes clean and readable.
- Groups related logic in one place.
- Makes your application easier to maintain.
- Works perfectly with middleware, validation, and services.
Conclusion:
Controllers are the backbone of Laravel applications after routes. They act as the middle layer between your routes and business logic.
Next, we’ll explore Views and Blade Templates so we can make our pages look good.

