0
0
LaravelConceptBeginner · 3 min read

API Resource Controller in Laravel: What It Is and How It Works

An API Resource Controller in Laravel is a special controller that automatically handles common RESTful API actions like listing, creating, updating, and deleting resources. It simplifies building APIs by providing predefined methods and routes for these actions, following Laravel's conventions.
⚙️

How It Works

Think of an API resource controller as a ready-made toolkit for managing data in your application through an API. Instead of writing separate methods for each action like showing all items, adding a new item, or deleting one, Laravel provides a controller with all these methods already set up.

When you create an API resource controller, Laravel expects you to define methods like index for listing data, store for saving new data, show for displaying a single item, update for changing data, and destroy for deleting data. Laravel then connects these methods to routes automatically, so your API follows a clear and consistent pattern.

This approach is like having a recipe book where each recipe is a method that handles a specific task, making your code organized and easy to maintain.

đź’»

Example

This example shows how to create an API resource controller for managing a Product resource and how Laravel maps HTTP requests to controller methods.

php
<?php

namespace App\Http\Controllers;

use App\Models\Product;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    public function index()
    {
        return Product::all();
    }

    public function store(Request $request)
    {
        $product = Product::create($request->all());
        return response()->json($product, 201);
    }

    public function show(Product $product)
    {
        return $product;
    }

    public function update(Request $request, Product $product)
    {
        $product->update($request->all());
        return response()->json($product, 200);
    }

    public function destroy(Product $product)
    {
        $product->delete();
        return response()->json(null, 204);
    }
}

// In routes/api.php
use App\Http\Controllers\ProductController;

Route::apiResource('products', ProductController::class);
Output
GET /api/products -> calls index() to list all products POST /api/products -> calls store() to create a new product GET /api/products/{product} -> calls show() to get a product PUT/PATCH /api/products/{product} -> calls update() to update a product DELETE /api/products/{product} -> calls destroy() to delete a product
🎯

When to Use

Use an API resource controller when you want to build a RESTful API that manages data resources in a clean and consistent way. It is perfect for applications that need to provide data to frontend apps, mobile apps, or other services.

For example, if you are creating an online store, you can use API resource controllers to handle products, orders, and customers. This saves time because you don’t have to write repetitive code for each action, and it keeps your API organized and easy to understand.

âś…

Key Points

  • An API resource controller provides predefined methods for common API actions.
  • Laravel automatically maps routes to these controller methods using Route::apiResource.
  • It helps keep your API code clean, consistent, and easy to maintain.
  • Use it when building RESTful APIs that manage resources like users, products, or posts.
âś…

Key Takeaways

API resource controllers simplify RESTful API development by providing standard methods for CRUD operations.
Laravel's Route::apiResource automatically connects HTTP verbs and URLs to controller methods.
Use API resource controllers to keep your API code organized and reduce repetitive coding.
They are ideal for managing resources like products, users, or orders in your application.