🚀 Laravel Controllers Explained with Examples (Beginner-Friendly)

Rohit DhimanRohit Dhiman
3 min read

If you're new to Laravel and struggling to understand how to manage route logic efficiently, this guide is for you. One of the first concepts to grasp in Laravel's MVC structure is the Controller.

Let’s break it down in simple terms with practical examples you can use today.


🧠 What is a Controller in Laravel?

A Controller is a PHP class that handles the logic for incoming HTTP requests. Instead of writing everything directly in your web.php routes file, you can organize your logic into controller methods.

Why it matters:

Writing logic in routes might work for very small projects, but as your application grows, it becomes messy and hard to maintain. Controllers help structure your application cleanly using the MVC pattern — Model, View, Controller.


🛠️ Creating a Controller

You can create a controller using Laravel’s artisan command:

php artisan make:controller PostController

This command generates a file:

app/Http/Controllers/PostController.php

Inside, you’ll define methods like index, show, etc., that will handle requests.


✏️ Example: Basic Controller Logic

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller
{
    public function index()
    {
        $posts = [
            ['id' => 1, 'title' => 'Laravel Basics'],
            ['id' => 2, 'title' => 'Understanding Controllers'],
            ['id' => 3, 'title' => 'Blade Templates 101'],
        ];

        return view('posts', compact('posts'));
    }

    public function show($id)
    {
        return "You are viewing post with ID: $id";
    }
}

🔗 Routing to Controllers

In your routes/web.php, map your routes like this:

use App\Http\Controllers\PostController;

Route::get('/posts', [PostController::class, 'index']);
Route::get('/posts/{id}', [PostController::class, 'show']);

Now:

  • /posts will call PostController@index

  • /posts/1 will call PostController@show with $id = 1


🖥️ Optional: Simple Blade View

In resources/views/posts.blade.php:

<!DOCTYPE html>
<html>
<head>
    <title>All Posts</title>
</head>
<body>
    <h1>Blog Posts</h1>
    <ul>
        @foreach ($posts as $post)
            <li>
                <a href="{{ url('/posts/' . $post['id']) }}">{{ $post['title'] }}</a>
            </li>
        @endforeach
    </ul>
</body>
</html>

Now visiting /posts will render the list of post titles with links.


📦 Bonus: Laravel Resource Controllers

Laravel also lets you create a resource controller that includes all CRUD methods out-of-the-box:

php artisan make:controller PostController --resource

You’ll get predefined methods like:

  • index()

  • create()

  • store()

  • show($id)

  • edit($id)

  • update($id)

  • destroy($id)

Register the resource route like this:

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

It maps all routes to appropriate controller methods following RESTful conventions.


💡 Benefits of Using Controllers

✅ Separation of logic from routes ✅ Code reusability and modularity ✅ Easier debugging and testing ✅ Scalable architecture ✅ Ideal for REST APIs


🧘 Final Thoughts

Understanding and using controllers is one of the best ways to write clean, manageable Laravel code. Whether you’re building a blog or a full-featured web app, controllers give you a reliable structure to grow with.

Start small:

  • Move logic out of routes

  • Create basic controller methods

  • Return Blade views or responses

And soon, Laravel will feel less like a framework and more like a superpower 💪


🧩 GitHub Example

Want a working demo? 👉 Laravel Controllers Example on GitHub


🙌 Let’s Connect!

If you enjoyed this article, share your thoughts or ask questions below. Follow me on LinkedIn / Substack / GitHub for more beginner-friendly Laravel tutorials.


🔖 Tags

#Laravel #PHP #WebDevelopment #MVC #CleanCode #LaravelBeginners

0
Subscribe to my newsletter

Read articles from Rohit Dhiman directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rohit Dhiman
Rohit Dhiman

Laravel developer with 10+ years experience building scalable backends, APIs, and full-stack systems. 💡 Expect posts about: Laravel design patterns Stripe & Twilio integrations RESTful API tips Docker for Laravel devs AWS setups for backend scaling Let’s learn & build together 🚀