Building a REST API with Laravel from Scratch
From routes to tidy JSON responses: we build a simple product API complete with validation.
C Codesakura 2分 コメント3件
目次
A REST API lets other apps (a mobile app, a React frontend, or a third-party service) talk to yours. In this part we build a simple product API: list, create, and delete.
Setting up the routes
Laravel ships with Route::apiResource, which registers every CRUD route at once. Add this to routes/api.php:
Route::apiResource('products', ProductController::class);Writing the controller
The controller simply returns models. Laravel turns them into JSON for you.
public function index()
{
return Product::latest()->paginate(20);
}Next steps
The API is still open to anyone. In the next part we add token authentication so only registered users can create products.
シリーズの一部
Laravel from Scratch
コメント3件
コメントするにはログインしてください。 ログイン
Dewi S.
Ditunggu lanjutannya.
Rizky A.
Penjelasannya runtut, langsung saya coba dan jalan. Terima kasih!
Codesakura 著者
Senang bisa membantu. Kalau ada yang macet, tulis saja di sini.