HomeLARAVELWhat are Controllers in Laravel? A Complete Guide

What are Controllers in Laravel? A Complete Guide

When building applications with Laravel, one of the most important concepts you’ll work with is Controllers. They act as the “middleman” between your application’s routes and business logic, making your code clean, organized, and easy to maintain.

In this blog, we’ll explore everything you need to know about Controllers in Laravel — from the basics to advanced usage.


🔹 What is a Controller in Laravel?

A Controller in Laravel is a PHP class that handles incoming HTTP requests and returns responses to the user. Instead of writing all your logic inside routes, you can move it to Controllers, keeping your code neat and structured.

👉 Think of Controllers as traffic managers. When a request hits your application, the route passes it to the controller, which then decides what logic to run and what response to send back.


🔹 Why Use Controllers?

  1. Organized Code – Keeps routes clean and moves logic into classes.
  2. Reusability – The same method can be reused across multiple routes.
  3. Separation of Concerns – Routes handle URLs, Controllers handle logic.
  4. Easier Testing – You can test controller methods individually.

🔹 Creating a Controller in Laravel

Laravel provides an Artisan command to generate a controller.

php artisan make:controller UserController

This will create a new file in:
app/Http/Controllers/UserController.php

Example of a basic controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index() {
        return "This is the user index page.";
    }
}

🔹 Using Controllers with Routes

Once a controller is created, you can define routes that point to its methods.

Example:

use App\Http\Controllers\UserController;

Route::get('/users', [UserController::class, 'index']);

Here:

  • /users → URL route
  • UserController → Controller class
  • index → Method inside the controller

🔹 Types of Controllers in Laravel

Laravel provides different types of controllers to handle various situations.

1. Basic Controllers

Handle simple logic like returning views or data.

public function about() {
    return view('about');
}

2. Resource Controllers

Used for CRUD operations (Create, Read, Update, Delete).

php artisan make:controller ProductController --resource

This generates methods like:

  • index() → List all items
  • create() → Show form
  • store() → Save data
  • show($id) → Display single item
  • edit($id) → Show edit form
  • update(Request $request, $id) → Update item
  • destroy($id) → Delete item

Registering a resource controller route:

Route::resource('products', ProductController::class);

3. Invokable Controllers

These controllers have only one method: __invoke().
Useful for single-action routes.

php artisan make:controller ContactController --invokable

Generated example:

class ContactController extends Controller
{
    public function __invoke() {
        return "This is a single action controller!";
    }
}

Route:

Route::get('/contact', ContactController::class);

4. API Controllers

When building APIs, Laravel provides --api option to generate controllers without unnecessary methods like create or edit.

php artisan make:controller Api/UserController --api

🔹 Passing Data to Controllers

Controllers can accept parameters from routes.

Route::get('/user/{id}', [UserController::class, 'show']);

Controller method:

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

🔹 Dependency Injection in Controllers

Laravel automatically injects dependencies into controller methods.

Example with Request:

public function store(Request $request) {
    $name = $request->input('name');
    return "User name: " . $name;
}

🔹 Middleware in Controllers

You can apply middleware directly inside controllers.

class DashboardController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index() {
        return view('dashboard');
    }
}

🔹 Best Practices for Controllers

  • Keep Controllers Thin – Move heavy logic to Services or Models.
  • Use Resource Controllers for CRUD – Helps maintain consistency.
  • Name Controllers Clearly – e.g., UserController, ProductController.
  • Leverage Middleware – For authentication, authorization, etc.

🔹 Conclusion

Controllers are the backbone of any Laravel application. They give structure, maintainability, and clarity to your code. Whether you’re building a basic website or a full-scale API, mastering controllers will help you build scalable applications with Laravel.

👉 In short:

  • Routes define what URL to hit
  • Controllers define what logic to run

⚡ Next Steps:
If you’re learning Laravel, your next topic should be:

  • [Laravel Routing Explained]
  • [Laravel Middleware Deep Dive]
  • [Laravel Models & Eloquent ORM]

Share: 

No comments yet! You be the first to comment.

Leave a Reply

Your email address will not be published. Required fields are marked *