Skip to content

Commit 88fbda2

Browse files
committed
Move restful api response helper to helpers
1 parent b5f80ab commit 88fbda2

File tree

3 files changed

+79
-72
lines changed

3 files changed

+79
-72
lines changed

application/controllers/api/v1/User.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct()
2222

2323
public function index()
2424
{
25-
$this->send_success_response($this->users);
25+
return send_success_response($this->users);
2626
}
2727

2828
public function show($id)
@@ -31,12 +31,12 @@ public function show($id)
3131

3232
if ($found !== FALSE) {
3333
$user = $this->users[$found];
34-
$this->send_success_response($user);
34+
return send_success_response($user);
3535
}
3636

37-
$this->send_response([
37+
return send_response([
3838
'success' => FALSE,
3939
'error' => 'There is no user with the given ID.'
40-
], self::HTTP_NOT_FOUND);
40+
], HTTP_NOT_FOUND);
4141
}
4242
}

application/core/MY_Controller.php

Lines changed: 7 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,6 @@
33

44
class MY_Controller extends CI_Controller {
55

6-
const HTTP_OK = 200;
7-
const HTTP_CREATED = 201;
8-
const HTTP_NOT_MODIFIED = 304;
9-
const HTTP_BAD_REQUEST = 400;
10-
const HTTP_UNAUTHORIZED = 401;
11-
const HTTP_FORBIDDEN = 403;
12-
const HTTP_NOT_FOUND = 404;
13-
const HTTP_METHOD_NOT_ALLOWED = 405;
14-
const HTTP_NOT_ACCEPTABLE = 406;
15-
const HTTP_UNPROCESSABLE_ENTITY = 422;
16-
const HTTP_INTERNAL_ERROR = 500;
17-
186
/**
197
* Defining if the request must an ajax request
208
*
@@ -26,10 +14,16 @@ class MY_Controller extends CI_Controller {
2614
public function __construct()
2715
{
2816
parent::__construct();
17+
18+
$this->load->helper('api');
2919

3020
$this->ajax_request_validator();
3121
}
3222

23+
/**
24+
* Checking if the current requested method
25+
* must be called by ajax
26+
*/
3327
private function ajax_request_validator()
3428
{
3529
$error = FALSE;
@@ -52,61 +46,6 @@ private function ajax_request_validator()
5246
}
5347
}
5448

55-
if ($error) self::send_bad_request('Ajax request only!');
56-
}
57-
58-
public static function send_response($data, $response_code = self::HTTP_OK)
59-
{
60-
http_response_code($response_code);
61-
header('Content-Type: application/json');
62-
echo json_encode($data);
63-
exit;
64-
}
65-
66-
public static function send_success_response($payload = FALSE)
67-
{
68-
$data['success'] = TRUE;
69-
70-
if ($payload !== FALSE) {
71-
$data['data'] = $payload;
72-
}
73-
74-
self::send_response($data, self::HTTP_OK);
75-
}
76-
77-
public static function send_unprocessable_entity($payload = FALSE)
78-
{
79-
$data['success'] = FALSE;
80-
$data['error'] = 'Unprocessable Entity';
81-
82-
if ($payload !== FALSE) {
83-
$data['data'] = $payload;
84-
}
85-
86-
self::send_response($data, self::HTTP_UNPROCESSABLE_ENTITY);
87-
}
88-
89-
public static function send_internal_server_error($error_message = FALSE)
90-
{
91-
$data['success'] = FALSE;
92-
$data['error'] = 'Internal Server Error';
93-
94-
if ($error_message !== FALSE) {
95-
$data['error'] = $error_message;
96-
}
97-
98-
self::send_response($data, self::HTTP_INTERNAL_ERROR);
99-
}
100-
101-
public static function send_bad_request($error_message = FALSE)
102-
{
103-
$data['success'] = FALSE;
104-
$data['error'] = 'Bad Request';
105-
106-
if ($error_message !== FALSE) {
107-
$data['error'] = $error_message;
108-
}
109-
110-
self::send_response($data, self::HTTP_BAD_REQUEST);
49+
if ($error) send_bad_request('Ajax request only!');
11150
}
11251
}

application/helpers/api_helper.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
const HTTP_OK = 200;
4+
const HTTP_CREATED = 201;
5+
const HTTP_NOT_MODIFIED = 304;
6+
const HTTP_BAD_REQUEST = 400;
7+
const HTTP_UNAUTHORIZED = 401;
8+
const HTTP_FORBIDDEN = 403;
9+
const HTTP_NOT_FOUND = 404;
10+
const HTTP_METHOD_NOT_ALLOWED = 405;
11+
const HTTP_NOT_ACCEPTABLE = 406;
12+
const HTTP_UNPROCESSABLE_ENTITY = 422;
13+
const HTTP_INTERNAL_ERROR = 500;
14+
15+
function send_response($data, $response_code = HTTP_OK)
16+
{
17+
http_response_code($response_code);
18+
header('Content-Type: application/json');
19+
echo json_encode($data);
20+
exit;
21+
}
22+
23+
function send_success_response($payload = FALSE)
24+
{
25+
$data['success'] = TRUE;
26+
27+
if ($payload !== FALSE) {
28+
$data['data'] = $payload;
29+
}
30+
31+
send_response($data, HTTP_OK);
32+
}
33+
34+
function send_unprocessable_entity($payload = FALSE)
35+
{
36+
$data['success'] = FALSE;
37+
$data['error'] = 'Unprocessable Entity';
38+
39+
if ($payload !== FALSE) {
40+
$data['data'] = $payload;
41+
}
42+
43+
send_response($data, HTTP_UNPROCESSABLE_ENTITY);
44+
}
45+
46+
function send_internal_server_error($error_message = FALSE)
47+
{
48+
$data['success'] = FALSE;
49+
$data['error'] = 'Internal Server Error';
50+
51+
if ($error_message !== FALSE) {
52+
$data['error'] = $error_message;
53+
}
54+
55+
send_response($data, HTTP_INTERNAL_ERROR);
56+
}
57+
58+
function send_bad_request($error_message = FALSE)
59+
{
60+
$data['success'] = FALSE;
61+
$data['error'] = 'Bad Request';
62+
63+
if ($error_message !== FALSE) {
64+
$data['error'] = $error_message;
65+
}
66+
67+
send_response($data, HTTP_BAD_REQUEST);
68+
}

0 commit comments

Comments
 (0)