PUT and PATCH Method difference Implementation in Code Igniter4 Tutorial

Certainly! In CodeIgniter 4, handling different HTTP methods like PUT and PATCH can be done by defining methods within your controller and handling the raw input data. Below is an example of a CodeIgniter 4 controller class that demonstrates how to handle both PUT and PATCH requests for updating user information.

Example Controller Class in CodeIgniter 4

php
<?php namespace App\Controllers; use CodeIgniter\RESTful\ResourceController; class UserController extends ResourceController { protected $modelName = 'App\Models\UserModel'; protected $format = 'json'; public function update($id = null) { if ($this->request->getMethod() === 'put') { return $this->updatePut($id); } elseif ($this->request->getMethod() === 'patch') { return $this->updatePatch($id); } else { return $this->failMethodNotAllowed('Only PUT and PATCH methods are allowed'); } } private function updatePut($id) { // Fetch the raw input data $input = $this->request->getRawInput(); // Assuming $input contains 'name' and 'email' fields $data = [ 'name' => $input['name'] ?? null, 'email' => $input['email'] ?? null, ]; // Validate the data (you can use CodeIgniter's validation here) if (empty($data['name']) || empty($data['email'])) { return $this->failValidationErrors('Name and email are required'); } // Update the user data in the model $model = new \App\Models\UserModel(); $model->update($id, $data); return $this->respond([ 'status' => 200, 'message' => 'User updated successfully with PUT method', ]); } private function updatePatch($id) { // Fetch the raw input data $input = $this->request->getRawInput(); // Prepare data for partial update $data = array_filter($input); // Only include non-null values // Validate the data (optional validation) if (empty($data)) { return $this->failValidationErrors('No valid data provided for update'); } // Update only the specified fields in the model $model = new \App\Models\UserModel(); $model->update($id, $data); return $this->respond([ 'status' => 200, 'message' => 'User updated successfully with PATCH method', ]); } }

Explanation

  1. update Method:

    • This method is a general entry point for update requests.
    • It checks the HTTP method (PUT or PATCH) and calls the appropriate method.
  2. updatePut Method:

    • Handles PUT requests.
    • Fetches the entire new data for the resource (user) and updates it.
    • The data must include all required fields (e.g., name and email).
  3. updatePatch Method:

    • Handles PATCH requests.
    • Fetches partial data for the resource (user) and updates only the fields provided in the request.
    • Uses array_filter to exclude null or empty values.
  4. Model Interaction:

    • UserModel is used to interact with the database. Make sure your UserModel extends CodeIgniter\Model and is properly configured to handle user data.

Model Example (UserModel)

Here’s a simple example of a UserModel:

php
<?php namespace App\Models; use CodeIgniter\Model; class UserModel extends Model { protected $table = 'users'; protected $primaryKey = 'id'; protected $allowedFields = ['name', 'email']; }

Summary

  • PUT: Replaces the entire resource with new data.
  • PATCH: Updates only specified fields of the resource.

By using PUT and PATCH methods appropriately, you ensure that your API adheres to standard practices and is both flexible and efficient.

Congratulations! You've successfully learned about put and patch method differences in codeigniter 4.