Cơ BảnLaravel iconLaravel

Controller trong Laravel là gì và có những method nào?

Controller xử lý logic request, thường tương ứng với một resource (User, Product). Tạo bằng php artisan make:controller ProductController --resource sinh ra các method CRUD: index() (hiển thị tất cả), show($id) (hiển thị một), create() (hiển thị form tạo), store() (lưu từ form), edit() (hiển thị form sửa), update() (lưu sửa đổi), destroy() (xóa). Controller xử lý request, tương tác với Model, và trả về View hay JSON.

Ví dụ: public function store(Request $request) { $product = Product::create($request->validated()); return redirect()->route("products.show", $product); }. Luôn dùng $request->validated() thay vì $request->all() để tránh mass assignment vulnerability.

Xem toàn bộ Laravel cùng filter theo level & chủ đề con.

Mở danh sách Laravel