DEV Community

Cover image for πŸš€ Laravel Controllers Explained with Examples (Beginner-Friendly)
Rohit Dhiman
Rohit Dhiman

Posted on

πŸš€ Laravel Controllers Explained with Examples (Beginner-Friendly)

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
Enter fullscreen mode Exit fullscreen mode

This command generates a file:

app/Http/Controllers/PostController.php
Enter fullscreen mode Exit fullscreen mode

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";
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ”— 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']);
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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

Top comments (2)

Collapse
 
jhunce profile image
jhunce

Nice for beginner..

Collapse
 
rohitdhiman profile image
Rohit Dhiman

I am happy that it helped you. Thanks!