Controllers in Laravel

πŸ“˜ Laravel πŸ‘ 62 views πŸ“… Dec 22, 2025
⏱ Estimated reading time: 2 min

Controllers in Laravel are responsible for handling incoming HTTP requests and returning responses. They act as an intermediary between routes and views/models, helping keep application logic organized and maintainable. Controllers are a core part of Laravel’s MVC (Model–View–Controller) architecture.


1. What Is a Controller?

A controller groups related request-handling logic into a single class. Instead of writing all logic inside route files, controllers help structure code in a clean and reusable way.

Example flow:

Request β†’ Route β†’ Controller β†’ Model/View β†’ Response

2. Creating a Controller

Laravel provides Artisan commands to create controllers.

php artisan make:controller UserController

This creates a controller file in:

app/Http/Controllers/UserController.php

3. Basic Controller Structure

Example of a simple controller:

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class UserController extends Controller { public function index() { return "User List"; } }

4. Connecting Controllers to Routes

To use a controller method, define it in a route:

use App\Http\Controllers\UserController; Route::get('/users', [UserController::class, 'index']);

When /users is accessed, the index method is executed.


5. Controllers with Views

Controllers commonly return views:

public function show() { return view('users'); }

The view file should be located at:

resources/views/users.blade.php

6. Passing Data to Views

Controllers can send data to views:

public function profile() { $name = "John Doe"; return view('profile', compact('name')); }

In the view:

{{ $name }}


7. Request Handling in Controllers

Laravel automatically injects the Request object:

use Illuminate\Http\Request; public function store(Request $request) { $name = $request->input('name'); return $name; }

8. Resource Controllers

Resource controllers handle CRUD operations:

php artisan make:controller PostController --resource

This generates methods like:

  • index

  • create

  • store

  • show

  • edit

  • update

  • destroy

Used with:

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

9. Middleware in Controllers

Middleware can be applied inside controllers:

public function __construct() { $this->middleware('auth'); }

This restricts access to authenticated users.


10. Single-Action Controllers

Controllers with one method use the __invoke method:

php artisan make:controller DashboardController --invokable

Example:

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

Conclusion

Controllers in Laravel help manage application logic efficiently by separating responsibilities. They improve code readability, reusability, and scalability, making Laravel applications easier to maintain and extend.


πŸ”’ Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes