Creating Your First Controller: Mastering Request Handling
Learn how to use controllers to clean up your Laravel routes, organize your code using the MVC pattern, and handle incoming requests like a pro.
Previously in this course, we explored defining basic web routes and learned how to capture dynamic values using route parameters. While writing logic directly inside your routes/web.php file is convenient for tiny scripts, it quickly becomes unmanageable as your project grows.
Today, we are taking a major step toward professional development by moving that logic into dedicated controllers.
Why Controllers? The MVC Principle
In the Model-View-Controller (MVC) architecture, the route's only job is to direct traffic. It should act as a signpost: "If a user visits this URL, send them to this specific piece of code."
When you dump business logic (like database queries or complex data processing) directly into the route file, you violate the "Single Responsibility Principle." Controllers act as the middleman that processes the request, interacts with your models, and decides which view to return. This separation makes your code easier to test, read, and maintain.
Generating Your First Controller
Laravel provides a powerful CLI tool called Artisan to handle the boilerplate for you. Instead of creating files manually, we use the make:controller command.
Open your terminal in your project root and run:
Bashphp artisan make:controller TaskController
This command creates a new file at app/Http/Controllers/TaskController.php. If you open that file, you’ll see a clean, empty class waiting for your logic.
Defining a Controller Method
A controller method is just a standard PHP function inside your controller class. Each method usually corresponds to a specific action, such as "show a list of tasks" or "create a new task."
Let’s add a simple method to our new controller:
PHP<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class TaskController extends Controller { public function index() { return "This is the list of all tasks!"; } }
Pointing a Route to a Controller Action
Now that we have the logic inside our controller, we need to update our routes/web.php file to point to it. Instead of passing a closure (the function() { ... } block), we pass an array containing the class name and the method name.
PHPuse App\Http\Controllers\TaskController; use Illuminate\Support\Facades\Route; Route::get('/tasks', [TaskController::class, 'index']);
When a user visits /tasks, Laravel now automatically instantiates TaskController and calls the index method. It is clean, declarative, and organized.
Hands-on Exercise
To practice this, we’ll advance our Task Manager project:
- Generate a controller named
TaskControllerusingphp artisan. - Add a method called
showto this controller that accepts an$idparameter. - Return a string that says "Displaying task with ID: " followed by the ID.
- Update your
routes/web.phpto map a GET route/tasks/{id}to thisshowmethod. - Verify it works by visiting
http://localhost:8000/tasks/5in your browser.
Common Pitfalls
- Forgetting the Import: Always ensure you have the
usestatement at the top of yourroutes/web.phpfile. If you don't importApp\Http\Controllers\TaskController, Laravel won't know which class you're talking about. - Method Mismatch: Ensure the method name in your route array exactly matches the function name in your controller. If you define
index()in the controller but point toshowin the route, you’ll see a "Method not found" error. - Logic Bloat: Even with controllers, it's easy to get carried away. If your controller method starts exceeding 20-30 lines, it's usually a sign that you should move that logic into a Service class or a Model method (we'll cover that later in the course).
Recap
By moving logic into controllers, you've adopted a standard architectural pattern that keeps your application scalable. You now know how to:
- Generate controllers using
php artisan make:controller. - Encapsulate logic within controller methods.
- Route incoming requests to these specific methods using
[Controller::class, 'method']syntax.
These skills are the foundation of professional request handling in Laravel.
Up next: We will learn how to return actual views and perform redirects, moving away from returning raw strings and toward building a real user interface.
Work with me

Laravel REST API Development
Clean, secure, well-documented Laravel REST APIs — the backend engine for your app, mobile client, or SaaS. Built by an API specialist.

FilamentPHP Admin Panel & Dashboard Development
A powerful admin panel for your Laravel app — built with FilamentPHP so you can manage everything without touching the database.