Skip to main content

What are controllers

Instead of defining all of your request handling logic as closures in your route files, you may wish to organize this behavior using “controller” classes. Controllers can group related request handling logic into a single class. For example, a UserController class might handle all incoming requests related to users, including showing, creating, updating, and deleting them. By default, controllers are stored in the app/Http/Controllers directory.

Creating a controller

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

Basic controllers

A controller class can have any number of public methods that respond to incoming HTTP requests.
Once you have written a controller class and method, you may define a route to the controller method like so:
When an incoming request matches the specified route URI, the show method on the App\Http\Controllers\UserController class will be invoked and the route parameter will be passed to the method.
Controllers are not required to extend a base class. However, it is sometimes convenient to extend a base controller class that contains methods that should be shared across all of your controllers.

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, define a single __invoke method within the controller.
When registering routes for single action controllers, you do not need to specify a controller method. Simply pass the name of the controller to the router.
You may generate an invokable controller by using the --invokable option on the make:controller Artisan command.

Resource controllers

If you think of each Eloquent model in your application as a “resource,” it is typical to perform the same sets of actions against each resource (CRUD). Laravel’s resource routing assigns the typical create, read, update, and delete (“CRUD”) routes to a controller with a single line of code. You may use the --resource option on the make:controller Artisan command to quickly create a controller to handle these actions.
This command generates a controller at app/Http/Controllers/PhotoController.php. The controller contains a method stub for each of the available resource operations. Next, register a resource route that points to the controller.
This single route declaration creates multiple routes to handle a variety of actions on the resource. The generated controller already has methods stubbed for each of these actions.
Running php artisan route:list provides a quick overview of your application’s routes.

Partial resource routes

When declaring a resource route, you may specify a subset of actions the controller should handle.

API resource routes

When declaring resource routes that will be consumed by APIs, you commonly want to exclude routes that present HTML templates, such as create and edit. The apiResource method automatically excludes these two routes.

Dependency injection

Constructor injection

The Laravel service container is used to resolve all Laravel controllers. As a result, you are able to type-hint any dependencies your controller may need in its constructor. The declared dependencies will automatically be resolved and injected into the controller instance.

Method injection

In addition to constructor injection, you may also type-hint dependencies on your controller’s methods. A common use case for method injection is injecting the Illuminate\Http\Request instance into your controller methods.
If your controller method also receives input from a route parameter, list your route arguments after your other dependencies. For example, if your route is defined as follows:
You can define the controller method so that it type-hints the Illuminate\Http\Request and accesses the id parameter like so:
Making the most of dependency injection makes it easier to swap in mocks during testing, improving the testability of your code.

Next steps

Routing

Review how to define routes and integrate them with controllers.
Last modified on July 13, 2026