• Fri, Mar 2026

Working with Laravel Controllers: Handling Web Requests the Right Way

Working with Laravel Controllers: Handling Web Requests the Right Way

This in-depth tutorial will teach you everything you need to know about Laravel controllers. You’ll learn step by step how to create controllers, connect them with routes, handle requests, use resource controllers, and follow best practices. Written in an educational and engaging style, this article is designed for beginners who want to master request handling in Laravel.

Introduction

Controllers are at the heart of Laravel’s MVC (Model-View-Controller) architecture. They are responsible for handling incoming requests, processing logic, and returning responses. Without controllers, your routes would be cluttered with logic, making your code messy and hard to maintain.

In this guide, we’ll explore controllers in Laravel in great detail. You’ll learn how to create, organize, and use controllers effectively to build clean, scalable applications. We’ll also cover resource controllers, API controllers, route model binding, dependency injection, and advanced techniques.

What Are Controllers?

In Laravel, a controller is a class that groups related request handling logic. Instead of writing all your logic in route files, you can keep it inside controllers for better organization.

Here’s a simple mental model:

  • Routes define which URL maps to which controller method.
  • Controllers decide what to do with the request.
  • Views or JSON return the response to the user.

Creating a Controller

Laravel provides an artisan command to generate controllers easily.

php artisan make:controller UserController    

This will create a file at:

app/Http/Controllers/UserController.php

Inside it, you’ll see a class like this:

namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
    //
}    

Defining Methods in a Controller

Let’s add a method to our controller:

// app/Http/Controllers/UserController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
    public function index()
    {
        return "This is the user index page.";
    }
}    

Now create a route pointing to this controller method:

// routes/web.php
use App\Http\Controllers\UserController;
Route::get('/users', [UserController::class, 'index']);    

Visiting /users in your browser will show the response from the controller.

Returning Views from Controllers

Instead of returning plain text, you can return a Blade view:

public function index()
{
    return view('users.index');
}    

Create a Blade view:

<!-- resources/views/users/index.blade.php -->
<!DOCTYPE html>
<html>
<head>
  <title>Users</title>
</head>
<body>
  <h1>User List</h1>
</body>
</html>
<!-- image: users_index_view.png -->

Passing Data to Views

Controllers can pass data to views like this:

public function index()
{
    $users = ['Alice', 'Bob', 'Charlie'];
    return view('users.index', compact('users'));
}    

Then in your view:

<!-- resources/views/users/index.blade.php -->
<!DOCTYPE html>
<html>
<head>
  <title>Users</title>
</head>
<body>
  &lt;h1&gt;User List&lt;/h1&gt;
</body>
</html>
<!-- image: users_index_view.png -->

Route Parameters in Controllers

Sometimes, routes have dynamic parameters like user IDs.

// routes/web.php
Route::get('/users/{id}', [UserController::class, 'show']);   

Define the show method:

public function show($id)
{
    return "User ID is " . $id;
}    

Route Model Binding

Laravel can automatically inject models into your controller methods.

// routes/web.php
Route::get('/users/{user}', [UserController::class, 'profile']);    
public function profile(User $user)
{
    return view('users.profile', compact('user'));
}    

Here, Laravel will automatically fetch the User model from the database.

Request Validation in Controllers

You can validate requests directly inside controllers:

public function store(Request $request)
{
    $validated = $request->validate([
        'name' => 'required|max:255',
        'email' => 'required|email|unique:users',
    ]);

    User::create($validated);

    return redirect('/users');
}    

Resource Controllers

Resource controllers simplify CRUD operations by generating common methods automatically.

php artisan make:controller PostController --resource    

This creates a controller with methods like:

  • index() – List all posts
  • create() – Show form to create post
  • store() – Save new post
  • show() – Show single post
  • edit() – Edit form
  • update() – Update post
  • destroy() – Delete post

Define a resource route:


Route::resource('posts', PostController::class);
    

RESTful Controller Table

Here’s a table mapping REST actions to controller methods:

HTTP VerbURIController MethodAction
GET/postsindex()Display all posts
GET/posts/createcreate()Show form
POST/postsstore()Save new post
GET/posts/{id}show()Show specific post
GET/posts/{id}/editedit()Show edit form
PUT/PATCH/posts/{id}update()Update existing post
DELETE/posts/{id}destroy()Delete post

API Controllers

For APIs, Laravel provides a flag to generate controllers without view methods:

php artisan make:controller Api/UserController --api    

This gives you a clean controller for handling JSON responses.

public function index()
{
    return User::all();
}    

Dependency Injection in Controllers

Laravel supports dependency injection directly in controller methods.

public function report(ReportService $reportService)
{
    return $reportService->generate();
}    

Middleware in Controllers

You can assign middleware to specific controller methods.

public function __construct()
{
    $this->middleware('auth')->only('edit', 'update');
}    

Best Practices for Controllers

  • Keep controllers lean – move business logic to services.
  • Group related routes into the same controller.
  • Use resource controllers for CRUD operations.
  • Leverage route model binding for cleaner code.
  • Validate requests inside controllers or form requests.

Conclusion

Laravel controllers make handling web requests organized, reusable, and maintainable. From basic controllers to resource controllers and APIs, you now understand how to use them effectively. By applying best practices, your Laravel applications will be cleaner and easier to scale.

Continue exploring controllers by combining them with models, services, and middleware, and you’ll unlock the full power of Laravel’s MVC architecture.

This website uses cookies to enhance your browsing experience. By continuing to use this site, you consent to the use of cookies. Please review our Privacy Policy for more information on how we handle your data. Cookie Policy