In Laravel, controllers are responsible for handling the logic of your application's HTTP requests. They help to organize and encapsulate the code that processes incoming requests and generates responses. Here's an overview of how controllers work in Laravel:
-
Creating a Controller:
- To create a new controller, you can use the
make:controller
Artisan command. For example, to create aUserController
, run the following command in your terminal:php artisan make:controller UserController
- This command will generate a new
UserController
class file in theapp/Http/Controllers
directory.
- To create a new controller, you can use the
-
Basic Controller Structure:
- A basic controller typically extends the base Laravel
Controller
class. - By convention, controller class names are in singular form and follow the
StudlyCase
naming convention. - Example basic controller structure:
-
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class UserController extends Controller {
// Controller methods
}
- A basic controller typically extends the base Laravel
-
Controller Methods:
- Controller methods correspond to different actions or routes in your application.
- Methods can accept an instance of the
Request
object to access the incoming request data. - Typically, controller methods return a response, such as a view, JSON response, or redirect.
- Example controller method returning a view:
public function index() {
$users = User::all();
return view('users.index', ['users' => $users]);
}
-
Route-Controller Mapping:
- To link a route to a controller method, you can define a route in a route file (
web.php
,api.php
, etc.) and specify the controller and method using theuses()
orcontroller()
method. - Example route definition mapping to a controller method:
use App\Http\Controllers\UserController;
Route::get('/users', [UserController::class, 'index']);
- To link a route to a controller method, you can define a route in a route file (
-
Dependency Injection:
- Laravel provides powerful dependency injection capabilities for controllers.
- You can type-hint dependencies in a controller's constructor or method to automatically resolve and inject them.
- Example controller method with dependency injection:
use App\Services\UserService;
class UserController extends Controller {
protected $userService;
public function __construct(UserService $userService) {
$this->userService = $userService;
}
public function index() {
$users = $this->userService->getAllUsers();
return view('users.index', ['users' => $users]);
}
}
-
RESTful Resource Controllers:
- Laravel provides resourceful routing and resource controllers for CRUD operations on a model.
- Resource controllers can be generated using the
make:controller
command with the--resource
option. - Example resource controller generation:
php artisan make:controller UserController --resource
- This command will generate a controller with methods for handling RESTful actions like
index()
,create()
,store()
,show()
,edit()
,update()
, anddestroy()
.
These are the key concepts and usage of controllers in Laravel. Controllers help to organize your application's logic and provide a separation of concerns between routes and business logic. They play a crucial role in handling HTTP requests and generating appropriate responses.