Skip to content

Commit b5f80ab

Browse files
committed
Add support for restful API
1 parent b9d8a7d commit b5f80ab

File tree

5 files changed

+189
-5
lines changed

5 files changed

+189
-5
lines changed

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@ Just a basic example how to integrating CodeIgniter 3 + Vue.js 3 + Vite with sup
44

55
If you loves CodeIgniter 3 & Vue.js, you must try this one to make your life easier!
66

7-
Some changes to make this works!
8-
7+
**Some changes to make this works!**
98
1. application/helpers/vite_helper.php
109
2. application/controllers/Vue.php
1110
3. application/config/routes.php
1211
4. application/views/index.vue.php
1312
5. frontend/vite.config.js
1413

15-
# Running the project
16-
14+
## Running the project
1715
1. Setting CodeIgniter base_url at application/config/config.php
1816
2. Open Terminal/CMD and enter to ```frontend``` directory
1917
3. Install vue project dependencies: ```npm install```
@@ -22,4 +20,16 @@ Some changes to make this works!
2220
6. Open the browser and go to the project address, e.g. ```http://localhost/codeigniter3-vuejs3-vite-boilerplate/```
2321
7. Enjoy!
2422

23+
## Features / ideas
24+
25+
I try to keep this project as simple as possible, so you can making a changes to suit your needs. No need to install a bunch of libraries for making something simple.
26+
27+
### Restful API support: response helper, ajax request validation ✅
28+
- application/config/routes.php
29+
- application/core/MY_Controller.php
30+
- application/controllers/api/*
31+
32+
### Middlewares
33+
- Not implemented yet!
34+
2535
Powered by [ngekoding.github.io](https://ngekoding.github.io)

application/composer.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "ngekoding/codeigniter3-vuejs3-vite-boilerplate",
3+
"description": "CodeIgniter 3 + Vue.js 3 + Vite with supported Hot Module Replacement (HMR)",
4+
"type": "project",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Nur Muhammad",
9+
"email": "blog.nurmuhammad@gmail.com"
10+
}
11+
],
12+
"require": {}
13+
}

application/config/routes.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,12 @@
5757
* Vue Routes
5858
* All routes with 'site' prefix will used by Vue
5959
*/
60-
6160
$route['^site?(.+)'] = 'vue';
61+
62+
/**
63+
* We will use manual routes only for the API calls
64+
* We also defined spesific HTTP methods
65+
*/
66+
$route['api/v1/users']['GET'] = 'api/v1/user';
67+
$route['api/v1/users/(:num)']['GET'] = 'api/v1/user/show/$1';
68+
$route['api/(.*)'] = '404';
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
defined('BASEPATH') OR exit('No direct script access allowed');
3+
4+
class User extends MY_Controller {
5+
6+
// All method must called by ajax request
7+
protected $ajax_request_only = TRUE;
8+
9+
// Just example data
10+
// In the real case, this data from the DB
11+
private $users = [
12+
['id' => 1, 'name' => 'Nur Muhammad'],
13+
['id' => 2, 'name' => 'Nabil Muhammad Firdaus'],
14+
['id' => 3, 'name' => 'Resqa Dahmurah'],
15+
['id' => 4, 'name' => 'Dian Febrianto'],
16+
];
17+
18+
public function __construct()
19+
{
20+
parent::__construct();
21+
}
22+
23+
public function index()
24+
{
25+
$this->send_success_response($this->users);
26+
}
27+
28+
public function show($id)
29+
{
30+
$found = array_search($id, array_column($this->users, 'id'));
31+
32+
if ($found !== FALSE) {
33+
$user = $this->users[$found];
34+
$this->send_success_response($user);
35+
}
36+
37+
$this->send_response([
38+
'success' => FALSE,
39+
'error' => 'There is no user with the given ID.'
40+
], self::HTTP_NOT_FOUND);
41+
}
42+
}

application/core/MY_Controller.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
defined('BASEPATH') OR exit('No direct script access allowed');
3+
4+
class MY_Controller extends CI_Controller {
5+
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+
18+
/**
19+
* Defining if the request must an ajax request
20+
*
21+
* Boolean: affecting to all methods
22+
* Array: affecting only the specified methods
23+
*/
24+
protected $ajax_request_only = FALSE;
25+
26+
public function __construct()
27+
{
28+
parent::__construct();
29+
30+
$this->ajax_request_validator();
31+
}
32+
33+
private function ajax_request_validator()
34+
{
35+
$error = FALSE;
36+
$is_ajax_request = $this->input->is_ajax_request();
37+
38+
if ($this->ajax_request_only === TRUE && !$is_ajax_request) {
39+
$error = TRUE;
40+
} elseif (is_array($this->ajax_request_only)) {
41+
// Make checking with case insensitive
42+
$ajax_request_only = array_map('strtolower', $this->ajax_request_only);
43+
44+
// Get current requested method
45+
$requested_method = $this->router->fetch_method();
46+
47+
if (!$is_ajax_request &&
48+
!empty($ajax_request_only) &&
49+
in_array(strtolower($requested_method), $ajax_request_only)
50+
) {
51+
$error = TRUE;
52+
}
53+
}
54+
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);
111+
}
112+
}

0 commit comments

Comments
 (0)