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, aUserController 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 themake: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.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.
--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.
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.
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 ascreate 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 theIlluminate\Http\Request instance into your controller methods.
Illuminate\Http\Request and accesses the id parameter like so:
Next steps
Routing
Review how to define routes and integrate them with controllers.