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.
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.
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.
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!"; } }
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.
To practice this, we’ll advance our Task Manager project:
TaskController using php artisan.show to this controller that accepts an $id parameter.routes/web.php to map a GET route /tasks/{id} to this show method.http://localhost:8000/tasks/5 in your browser.use statement at the top of your routes/web.php file. If you don't import App\Http\Controllers\TaskController, Laravel won't know which class you're talking about.index() in the controller but point to show in the route, you’ll see a "Method not found" error.By moving logic into controllers, you've adopted a standard architectural pattern that keeps your application scalable. You now know how to:
php artisan make:controller.[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.
Learn how to use database seeding and factories in Laravel to populate your application with realistic dummy data for testing and development.
Read moreEloquent custom casts let you clean up your Laravel models by automatically transforming data. Learn how to handle complex types without the boilerplate.
Creating Your First Controller
Protecting Routes with Middleware
Understanding CSRF Protection
Preventing Mass Assignment
Task Manager: Securing the Application
Introduction to Route Model Binding
Updating Existing Records
Deleting Records
Using Named Routes
Task Manager: Completing CRUD Functionality
Introduction to Database Relationships
Querying Related Data
Handling File Uploads
Using Flash Messages for User Feedback
Task Manager: Adding Status and Priorities
Introduction to Artisan Commands
Debugging with Laravel Tinker
Understanding Service Providers
Using View Composers
Task Manager: Refactoring for Clean Code
Introduction to Testing
Testing Forms and Validation
Using Database Transactions
Handling Global Exceptions
Preparing for Production
Environment Security Best Practices
Managing Assets in Production
Task Manager: Deployment Preparation