Back to Blog
Lesson 8 of the Laravel Fundamentals: From Zero to Your First App course
LaravelJune 25, 20263 min read

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.

laravelphpbackend

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:

Bash
php 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.

PHP
use 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:

  1. Generate a controller named TaskController using php artisan.
  2. Add a method called show to this controller that accepts an $id parameter.
  3. Return a string that says "Displaying task with ID: " followed by the ID.
  4. Update your routes/web.php to map a GET route /tasks/{id} to this show method.
  5. Verify it works by visiting http://localhost:8000/tasks/5 in your browser.

Common Pitfalls

  • Forgetting the Import: Always ensure you have the 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.
  • 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 to show in 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.

Similar Posts