Laravel

Laravel Contrallers

Laravel Controllers: Organizing Your Application Logic

When building a Laravel application, you want your code to be organized, maintainable, and clean. That’s where Controllers come in.

Controllers act as the bridge between your application’s routes and your business logic. Instead of writing all logic directly inside routes, you delegate that work to controllers, making your app easier to manage and scale.

Let’s dive into what Laravel Controllers are and how to use them.


What Is a Controller?

A Controller is a PHP class that groups related request handling logic into a single place. For example, a PostController might handle creating, updating, deleting, and showing blog posts.

This helps separate concerns:

  • Routes handle URLs and HTTP verbs

  • Controllers handle processing and responses


Why Use Controllers?

  • Keep routes clean and readable

  • Reuse code easily

  • Improve testability

  • Group related actions logically


Creating a Controller

Using Artisan CLI

Laravel makes it easy to create controllers:

bash
php artisan make:controller PostController

This generates a PostController.php file inside app/Http/Controllers.


Basic Controller Example

php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller
{
// Show all posts
public function index()
{
return 'Displaying all posts';
}

// Show form to create a new post
public function create()
{
return 'Show create post form';
}

// Store a new post
public function store(Request $request)
{
// Validate and save post logic here
return 'Saving new post';
}

// Show a single post
public function show($id)
{
return "Displaying post with ID: $id";
}

// Show form to edit post
public function edit($id)
{
return "Edit form for post $id";
}

// Update a post
public function update(Request $request, $id)
{
return "Updating post $id";
}

// Delete a post
public function destroy($id)
{
return "Deleting post $id";
}
}


Connecting Routes to Controller Methods

In routes/web.php, link URLs to controller methods:

php
use App\Http\Controllers\PostController;

Route::get('/posts', [PostController::class, 'index']);
Route::get('/posts/create', [PostController::class, 'create']);
Route::post('/posts', [PostController::class, 'store']);
Route::get('/posts/{id}', [PostController::class, 'show']);
Route::get('/posts/{id}/edit', [PostController::class, 'edit']);
Route::put('/posts/{id}', [PostController::class, 'update']);
Route::delete('/posts/{id}', [PostController::class, 'destroy']);


Resource Controllers

Laravel offers a shortcut for defining all common CRUD routes:

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

This single line generates all the routes shown above, following Laravel conventions.


Controller Middleware

You can attach middleware to controllers to filter requests:

php
class PostController extends Controller
{
public function __construct()
{
$this->middleware('auth'); // Only authenticated users can access these methods
}

// Controller methods...
}


Final Thoughts

Controllers keep your Laravel apps organized and maintainable. By grouping related logic, you separate concerns, improve readability, and simplify testing.

If you’re just starting with Laravel, mastering controllers will give you a solid foundation for building scalable applications.

Leave a Reply

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