- Create App\Traits\ResponseTrait that wraps CodeIgniter\API\ResponseTrait - Add json_helper with convert_empty_strings_to_null() and prepare_json_response() functions - Replace all imports of CodeIgniter\API\ResponseTrait with App\Traits\ResponseTrait across all controllers - Add 'json' helper to BaseController helpers array - Ensure consistent API response formatting across the application
32 lines
788 B
PHP
32 lines
788 B
PHP
<?php
|
|
|
|
namespace App\Traits;
|
|
|
|
use CodeIgniter\API\ResponseTrait as BaseResponseTrait;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
|
|
trait ResponseTrait
|
|
{
|
|
use BaseResponseTrait {
|
|
respond as baseRespond;
|
|
}
|
|
|
|
/**
|
|
* Send a response with automatic conversion of empty strings to null
|
|
*
|
|
* @param mixed $data
|
|
* @param int $status
|
|
* @param string $message
|
|
* @return ResponseInterface
|
|
*/
|
|
public function respond($data = null, int $status = 200, string $message = '')
|
|
{
|
|
// Convert empty strings to null in the data
|
|
if ($data !== null && (is_array($data) || is_object($data))) {
|
|
$data = convert_empty_strings_to_null($data);
|
|
}
|
|
|
|
return $this->baseRespond($data, $status, $message);
|
|
}
|
|
}
|