Laravel Queue

🧠 Ultimate Guide to Laravel Controllers (Chapter 4)

In Laravel, Controllers play a crucial role in handling user requests and returning responses. If you’re developing a web application using Laravel, understanding controllers is essential to building clean, organized, and scalable applications.

In this article, we’ll cover:

  • What is a Controller in Laravel?
  • Why use Controllers?
  • Types of Laravel Controllers
  • How to Create a Controller in Laravel
  • Resource Controllers
  • API Controllers
  • Route-Controller Binding
  • Best Practices
  • Real-life Examples

📌 What is a Controller in Laravel?

A Controller in Laravel is a PHP class that handles incoming HTTP requests and returns responses. It acts as a bridge between the Model and View, following the MVC (Model-View-Controller) architecture.

Instead of writing all logic in your route closures, controllers help you organize code better, especially for large-scale applications.


⚙️ Why Use Controllers?

  • Keeps your routes clean and readable
  • Separates business logic from route definitions
  • Enhances code reusability and testability
  • Makes the application more maintainable

🛠️ How to Create a Controller in Laravel

You can generate a controller using Laravel Artisan command:

bashCopyEditphp artisan make:controller BlogController

This creates a new controller inside app/Http/Controllers/.


🧩 Types of Controllers in Laravel

1. Basic Controllers

These handle standard request/response logic.

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class BlogController extends Controller
{
public function index() {
return view('blog.index');
}

public function show($id) {
return view('blog.show', compact('id'));
}
}

2. Single-Action Controllers

Ideal when your controller has only one job.

php artisan make:controller ContactFormController --invokable
public function __invoke()
{
return view('contact');
}

3. Resource Controllers

Resource controllers provide default CRUD methods like index, create, store, show, update, and destroy.

bashCopyEditphp artisan make:controller ProductController --resource

Then define in routes:

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

🔗 Route to Controller Binding

You can define routes to call specific controller methods:

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

🌐 API Controllers

For APIs, Laravel provides --api option:

php artisan make:controller Api/UserController --api

This excludes view-based methods and keeps things API-specific (index, store, update, destroy, show).


✅ Best Practices for Laravel Controllers

  1. Keep controllers thin: Move heavy logic to Service classes or Models.
  2. Use Form Request classes for validation instead of validating directly in the controller.
  3. Group related routes using Route groups and namespaces.
  4. Use Route Model Binding for clean parameter injection.
  5. Don’t return raw data – always use response wrappers for APIs.

🧪 Real-World Example: BlogController

class BlogController extends Controller
{
public function index() {
$posts = Post::latest()->paginate(10);
return view('blog.index', compact('posts'));
}

public function show(Post $post) {
return view('blog.show', compact('post'));
}
}

And the route:

Route::get('/blogs', [BlogController::class, 'index']);
Route::get('/blogs/{post}', [BlogController::class, 'show']);

🔚 Conclusion

Laravel Controllers are essential for building scalable, clean, and efficient applications. Whether you’re building a basic blog or a complex SaaS platform, using controllers effectively will enhance your code structure and maintainability.

https://laravel.com/docs/11.x/controllers#main-content

Laravel Routes: A Complete Guide for Beginners and Experts (Chapter 5)

Leave a Reply

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