Documentation Index
Fetch the complete documentation index at: https://kawax.biz/llms.txt
Use this file to discover all available pages before exploring further.
What is a controller?
Instead of defining all your request handling logic as closures in route files, you can organize this behavior using “controller” classes. Controllers group related request handling logic into a single class. For example, aUserController class might handle all incoming requests related to users — displaying, creating, updating, and deleting them. By default, controllers are stored in the app/Http/Controllers directory.
Creating a controller
Use themake:controller Artisan command to quickly generate a new controller:
Basic controllers
A controller class can have any number of public methods that respond to incoming HTTP requests:show method on UserController and passes the route parameters to it.
Controllers do not need to extend a base class. However, extending a base controller class is useful when you want to share methods across all your controllers.
Single-action controllers
When a controller action is particularly complex, you may want to dedicate an entire controller class to that single action. To do so, define a single__invoke method inside the controller:
--invokable option to generate an invokable controller:
Resource controllers
Each Eloquent model in your application can be thought of as a “resource”, and it is common to perform the same set of actions (CRUD) against each resource. Laravel’s resource routing assigns typical create, read, update, and delete routes to a controller with a single line of code. Use the--resource option to generate a controller with stub methods for each of the resource actions:
| HTTP verb | URI | Action | Route name |
|---|---|---|---|
| GET | /photos | index | photos.index |
| GET | /photos/create | create | photos.create |
| POST | /photos | store | photos.store |
| GET | /photos/{photo} | show | photos.show |
| GET | /photos/{photo}/edit | edit | photos.edit |
| PUT/PATCH | /photos/{photo} | update | photos.update |
| DELETE | /photos/{photo} | destroy | photos.destroy |
Run
php artisan route:list to see an overview of all routes registered in your application.Partial resource routes
When declaring a resource route, you can specify a subset of actions the controller should handle:API resource routes
When declaring resource routes for an API, you typically want to exclude routes that present HTML templates, such ascreate and edit. Use apiResource to exclude both automatically:
Dependency injection
Constructor injection
Laravel’s service container resolves all Laravel controllers. You can type-hint any dependencies your controller needs in its constructor, and they will be automatically resolved and injected:Method injection
In addition to constructor injection, you can type-hint dependencies on your controller methods. A common use case is injecting theIlluminate\Http\Request instance:
Next steps
Routing
Review how to define routes and connect them to controllers.