What is Laravel - Controllers

Himmat Kumar Dec 24, 2024, 5:52 AM
Laravel
Views 308
Blog Thumbnail

What Are Controllers in Laravel?

Controllers are classes defined in the App\Http\Controllers namespace. They group related request-handling logic into a single class, improving code organization.

Types of Controllers

Single-Action Controllers

Controllers with a single method, typically invoked using the __invoke method.

php artisan make:controller SingleActionController --invokable
                

Resource Controllers

Basically Resource controllers create for crud operations (Create, Read, Update, Delete) operations.

php artisan make:controller BlogController --resource
                

Creating Controllers

To create a new controller:

php artisan make:controller BlogController
            

Routes and Controllers

You can map routes to controller methods using Route::get, Route::post, etc.

use App\Http\Controllers\BlogController;

Route::get('/blogs', [BlogController::class, 'index']);
            

For resource routes:

use App\Http\Controllers\BlogController;

Route::resource('blogs', BlogController::class);
            

Controller Methods

A standard resource controller includes the following methods:

  • index() - Display a listing of the resource.
  • create() - Show the form for creating a new resource.
  • store() - Save a newly created resource in storage.
  • show($id) - Display the specified resource.
  • edit($id) - Show the form for editing the specified resource.
  • update($id) - Update the specified resource in storage.
  • destroy($id) - Remove the specified resource from storage.

Example: Blog Controller

namespace App\Http\Controllers;

use App\Models\Blog;
use Illuminate\Http\Request;

class BlogController extends Controller
{
    public function index()
    {
        $blogs = Blog::latest()->get();
        return view('blogs.index', compact('blogs'));
    }

    public function show($id)
    {
        $blog = Blog::findOrFail($id);
        return view('blogs.show', compact('blog'));
    }
}
            

Integrating with Tailwind CSS

Tailwind CSS Blog Index View:

        <!DOCTYPE html>
        <html lang="en">
        <head>
            <title>Blogs</title>
            <script src="https://cdn.tailwindcss.com"></script>
        </head>
        <body class="bg-gray-100 text-gray-800">
            <div class="container mx-auto px-4 py-8">
                <h1 class="text-4xl font-bold mb-8 text-center">Blog Posts</h1>
                <div class="grid gap-6 md:grid-cols-2 lg:grid-cols-3">
                    @foreach($blogs as $blog)
                    <div class="bg-white rounded-lg shadow-md overflow-hidden">
                        <img src="{{ $blog->thumbnail }}" alt="Thumbnail" class="w-full h-48 object-cover">
                        <div class="p-4">
                            <h2 class="text-2xl font-semibold mb-2">{{ $blog->title }}</h2>
                            <p class="text-gray-600 mb-4">{{ Str::limit($blog->description, 100) }}</p>
                            <a href="{{ route('blogs.show', $blog->id) }}" class="text-blue-500 hover:underline">
                                Read More
                            </a>
                        </div>
                    </div>
                    @endforeach
                </div>
            </div>
        </body>
        </html>
            

Best Practices

  • Use resource controllers for RESTful APIs.
  • Group routes by functionality for better organization.
  • Use middleware for authentication and authorization.
  • Avoid placing business logic in controllers—use services or repositories instead.
  • Utilize route model binding for cleaner code.

Comments

Please login to leave a comment.

No comments yet.

Related Posts

laravel-configuration
228 viewsLaravel
Himmat Kumar Dec 4, 2024, 11:58 AM

Laravel Configuration

laravel-application-structure
185 viewsLaravel
Himmat Kumar Dec 3, 2024, 8:33 AM

Laravel Application File Structure

get-route-in-laravel
202 viewsLaravel
Himmat Kumar Dec 13, 2024, 11:07 AM

GET Route in Laravel

eloquent-relationships-guide
1089 viewsLaravel
Himmat Regar May 31, 2025, 7:32 PM

Mastering Eloquent Relationships in Laravel (2025) — Co...

laravel-setup-tutorial
290 viewsLaravel
Himmat Kumar Jul 30, 2024, 12:03 PM

Master Laravel: Basic System Requirement,Installation a...

laravel-service-container-guide
445 viewsLaravel
Himmat Regar May 31, 2025, 7:23 PM

Mastering Laravel Service Container: Dependency Injecti...

git-installation-guide-windows
172 viewsLaravel
Himmat Kumar Jan 3, 2024, 1:15 AM

Git Installation: Step-by-Step Guide for Windows - Lear...

laravel-cookies-guide
1094 viewsLaravel
Himmat Regar Jun 1, 2025, 2:23 PM

Laravel Cookies: Secure, Encrypted & Easy (Guide 2025)

how-to-send-emails-with-queues-in-laravel
148 viewsLaravel
Himmat Kumar Oct 16, 2024, 12:32 PM

How to Send Emails with Queues in Laravel. how to use ...

laravel-csrf-protection-guide
1162 viewsLaravel
Himmat Regar Jun 2, 2025, 6:23 PM

Laravel CSRF Protection Explained – Tokens, Middleware ...