5 Laravel 11 Basics: Controllers

Anis MarrouchiAbdoullah SghaierAI Bot
By Anis Marrouchi & Abdoullah Sghaier & AI Bot ·

Loading the Text to Speech Audio Player...

In Laravel, controllers allow you to group related request handling logic into a single class. This makes your code more organized and easier to maintain. This tutorial will cover the basics of creating and using controllers in Laravel 11.

Introduction to Controllers

Instead of defining all of your request handling logic as closures in your route files, you can organize this behavior using controller classes. Controllers can group related request handling logic into a single class. For example, a UserController might handle all incoming requests related to users, including showing, creating, updating, and deleting users.

Writing Controllers

Basic Controllers

To quickly generate a new controller, use the make:controller Artisan command. By default, controllers are stored in the app/Http/Controllers directory:

php artisan make:controller UserController

A basic controller may have any number of public methods that respond to incoming HTTP requests:

namespace App\Http\Controllers;
 
use App\Models\User;
use Illuminate\Http\Request;
 
class UserController extends Controller
{
    public function show($id)
    {
        return view('user.profile', ['user' => User::findOrFail($id)]);
    }
}

Once you have written a controller class and method, you can define a route to the controller method like so:

use App\Http\Controllers\UserController;
 
Route::get('/user/{id}', [UserController::class, 'show']);

When an incoming request matches the specified route URI, the show method on the UserController class will be invoked, and the route parameters will be passed to the method.

Single Action Controllers

If a controller action is particularly complex, you might find it convenient to dedicate an entire controller class to that single action. To accomplish this, you can define a single __invoke method within the controller:

namespace App\Http\Controllers;
 
use App\Models\User;
use Illuminate\Http\Request;
 
class ShowProfile extends Controller
{
    public function __invoke($id)
    {
        return view('user.profile', ['user' => User::findOrFail($id)]);
    }
}

You can then register the controller with the route like so:

use App\Http\Controllers\ShowProfile;
 
Route::get('/user/{id}', ShowProfile::class);

Controller Middleware

To assign middleware to a controller, use the middleware method within your controller's constructor. Middleware can be assigned to a specific controller action or to the entire controller:

namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
 
class UserController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
 
    public function show($id)
    {
        return view('user.profile', ['user' => User::findOrFail($id)]);
    }
}

Resource Controllers

Resource controllers make it easy to handle all common CRUD operations for a resource in a single line of code. To generate a resource controller, use the --resource option:

php artisan make:controller PhotoController --resource

This command will generate a controller with methods for handling all CRUD operations. You can register a resource controller route using the Route::resource method:

use App\Http\Controllers\PhotoController;
 
Route::resource('photos', PhotoController::class);

Learn more about Laravel controllers from the official Laravel documentation.

Conclusion

Controllers are a powerful feature in Laravel that help keep your code organized and manageable. This tutorial covered how to create and use controllers in Laravel 11. For more detailed information, refer to the official Laravel documentation.

Happy coding with Laravel 11!


Want to read more tutorials? Check out our latest tutorial on How to Open a Free Flouci Professional Account for Self-Employed Individuals in Tunisia.

Discuss Your Project with Us

We're here to help with your web development needs. Schedule a call to discuss your project and how we can assist you.

Let's find the best solutions for your needs.