Skip to content

Commit

Permalink
Move restful api response helper to helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ngekoding committed May 23, 2021
1 parent b5f80ab commit 88fbda2
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 72 deletions.
8 changes: 4 additions & 4 deletions application/controllers/api/v1/User.php
Expand Up @@ -22,7 +22,7 @@ public function __construct()

public function index()
{
$this->send_success_response($this->users);
return send_success_response($this->users);
}

public function show($id)
Expand All @@ -31,12 +31,12 @@ public function show($id)

if ($found !== FALSE) {
$user = $this->users[$found];
$this->send_success_response($user);
return send_success_response($user);
}

$this->send_response([
return send_response([
'success' => FALSE,
'error' => 'There is no user with the given ID.'
], self::HTTP_NOT_FOUND);
], HTTP_NOT_FOUND);
}
}
75 changes: 7 additions & 68 deletions application/core/MY_Controller.php
Expand Up @@ -3,18 +3,6 @@

class MY_Controller extends CI_Controller {

const HTTP_OK = 200;
const HTTP_CREATED = 201;
const HTTP_NOT_MODIFIED = 304;
const HTTP_BAD_REQUEST = 400;
const HTTP_UNAUTHORIZED = 401;
const HTTP_FORBIDDEN = 403;
const HTTP_NOT_FOUND = 404;
const HTTP_METHOD_NOT_ALLOWED = 405;
const HTTP_NOT_ACCEPTABLE = 406;
const HTTP_UNPROCESSABLE_ENTITY = 422;
const HTTP_INTERNAL_ERROR = 500;

/**
* Defining if the request must an ajax request
*
Expand All @@ -26,10 +14,16 @@ class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();

$this->load->helper('api');

$this->ajax_request_validator();
}

/**
* Checking if the current requested method
* must be called by ajax
*/
private function ajax_request_validator()
{
$error = FALSE;
Expand All @@ -52,61 +46,6 @@ private function ajax_request_validator()
}
}

if ($error) self::send_bad_request('Ajax request only!');
}

public static function send_response($data, $response_code = self::HTTP_OK)
{
http_response_code($response_code);
header('Content-Type: application/json');
echo json_encode($data);
exit;
}

public static function send_success_response($payload = FALSE)
{
$data['success'] = TRUE;

if ($payload !== FALSE) {
$data['data'] = $payload;
}

self::send_response($data, self::HTTP_OK);
}

public static function send_unprocessable_entity($payload = FALSE)
{
$data['success'] = FALSE;
$data['error'] = 'Unprocessable Entity';

if ($payload !== FALSE) {
$data['data'] = $payload;
}

self::send_response($data, self::HTTP_UNPROCESSABLE_ENTITY);
}

public static function send_internal_server_error($error_message = FALSE)
{
$data['success'] = FALSE;
$data['error'] = 'Internal Server Error';

if ($error_message !== FALSE) {
$data['error'] = $error_message;
}

self::send_response($data, self::HTTP_INTERNAL_ERROR);
}

public static function send_bad_request($error_message = FALSE)
{
$data['success'] = FALSE;
$data['error'] = 'Bad Request';

if ($error_message !== FALSE) {
$data['error'] = $error_message;
}

self::send_response($data, self::HTTP_BAD_REQUEST);
if ($error) send_bad_request('Ajax request only!');
}
}
68 changes: 68 additions & 0 deletions application/helpers/api_helper.php
@@ -0,0 +1,68 @@
<?php

const HTTP_OK = 200;
const HTTP_CREATED = 201;
const HTTP_NOT_MODIFIED = 304;
const HTTP_BAD_REQUEST = 400;
const HTTP_UNAUTHORIZED = 401;
const HTTP_FORBIDDEN = 403;
const HTTP_NOT_FOUND = 404;
const HTTP_METHOD_NOT_ALLOWED = 405;
const HTTP_NOT_ACCEPTABLE = 406;
const HTTP_UNPROCESSABLE_ENTITY = 422;
const HTTP_INTERNAL_ERROR = 500;

function send_response($data, $response_code = HTTP_OK)
{
http_response_code($response_code);
header('Content-Type: application/json');
echo json_encode($data);
exit;
}

function send_success_response($payload = FALSE)
{
$data['success'] = TRUE;

if ($payload !== FALSE) {
$data['data'] = $payload;
}

send_response($data, HTTP_OK);
}

function send_unprocessable_entity($payload = FALSE)
{
$data['success'] = FALSE;
$data['error'] = 'Unprocessable Entity';

if ($payload !== FALSE) {
$data['data'] = $payload;
}

send_response($data, HTTP_UNPROCESSABLE_ENTITY);
}

function send_internal_server_error($error_message = FALSE)
{
$data['success'] = FALSE;
$data['error'] = 'Internal Server Error';

if ($error_message !== FALSE) {
$data['error'] = $error_message;
}

send_response($data, HTTP_INTERNAL_ERROR);
}

function send_bad_request($error_message = FALSE)
{
$data['success'] = FALSE;
$data['error'] = 'Bad Request';

if ($error_message !== FALSE) {
$data['error'] = $error_message;
}

send_response($data, HTTP_BAD_REQUEST);
}

0 comments on commit 88fbda2

Please sign in to comment.