Laravel Resourceful Controllers for RESTful API Development
Course Title: Mastering Laravel Framework: Building Scalable Modern Web Applications Section Title: RESTful API Development with Laravel Topic: Building APIs in Laravel with resourceful controllers
Introduction to Resourceful Controllers
In Laravel, resourceful controllers are used to handle CRUD (Create, Read, Update, Delete) operations for a specific resource. They provide a simple and structured way to organize API endpoints and implement business logic. In this topic, we will explore how to build APIs in Laravel using resourceful controllers.
Creating Resourceful Controllers
To create a resourceful controller, use the --resource
flag with the make:controller
command:
php artisan make:controller --resource PostController
This command creates a PostController
class that extends the Controller
class and includes methods for handling CRUD operations.
Controller Methods
A resourceful controller includes the following methods:
index()
: Handles GET requests to retrieve a list of resources.create()
: Handles GET requests to create a new resource (returns a view with a form).store()
: Handles POST requests to create a new resource.show()
: Handles GET requests to retrieve a specific resource.edit()
: Handles GET requests to edit an existing resource (returns a view with a form).update()
: Handles PATCH or PUT requests to update an existing resource.destroy()
: Handles DELETE requests to delete a resource.
Example Controller
Here's an example PostController
class that includes methods for handling CRUD operations:
// app/Http/Controllers/PostController.php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function index()
{
$posts = Post::all();
return response()->json($posts);
}
public function create()
{
// Not implemented for API only
}
public function store(Request $request)
{
$post = Post::create($request->all());
return response()->json($post, 201);
}
public function show(Post $post)
{
return response()->json($post);
}
public function edit(Post $post)
{
// Not implemented for API only
}
public function update(Request $request, Post $post)
{
$post->update($request->all());
return response()->json($post);
}
public function destroy(Post $post)
{
$post->delete();
return response()->json(null, 204);
}
}
Routes for Resourceful Controllers
To define routes for a resourceful controller, use the Route::resource()
method:
// routes/api.php
Route::resource('posts', PostController::class);
This defines routes for the PostController
class and maps them to the following URIs:
GET /posts
: calls theindex()
methodPOST /posts
: calls thestore()
methodGET /posts/{post}
: calls theshow()
methodPUT|PATCH /posts/{post}
: calls theupdate()
methodDELETE /posts/{post}
: calls thedestroy()
method
Partially generated resourceful controllers
Sometimes you don't want to generate the entire resourceful controllers for CRUD operations. Laravel provides an easy way to generate specific routes:
Route::get('resourceUri', 'name.of.the.get.method');
Route::store('resourceUri', 'name.of.the.store.method');
Route::update('resourceUri', 'name.of.the.update.method');
Route::delete('resourceUri', 'name.of.the.destroy.method');
- More Options
To learn more, you can check the Laravel documentation for resourceful controllers and API routes.
Try to:
- Create a resourceful controller for handling CRUD operations for the
Post
model. - Define routes for the
PostController
class using theRoute::resource()
method.
Don't forget to leave a comment if you have any questions or if you'd like us to elaborate on any of the concepts!
Next Topic: Handling API requests and responses (JSON, XML) in RESTful API Development with Laravel.
Images

Comments