CodeIgniter 4 RESTful & Auth Api
CodeIgniter 4 RESTful & Auth Api Resource Base Controller
This RESTful API extension is collected into composer require meksiabdou/ci4-restful which is a complete solution for Codeigniter framework.
Features
-
Auth use package myth/auth
-
RESTful API implementation
-
Logs Requests
OUTLINE
DEMONSTRATION
use CI4Restful\Helpers\RestServer;
class ApiController extends RestServer
{
public function index()
{
return $this->response_json(["id" => 1, "bar" => "foo" ]], true);
}
}
Output with status 200 OK
:
{
"results" : {
"id": 1,
"bar" : "foo",
},
"status" : true,
}
RESTful Create Function
public function store($requestData=null) {
$this->db->insert('mytable', $requestData);
$id = $this->db->insert_id();
return $this->response->json(['id'=>$id], true , 201);
}
Output with status 201
:
{
"results" : {
"id": 1,
},
"status" : true,
}
Output with error
public function store($requestData=null) {
if(!$requestData) {
return $this->response->json([], false );
}
}
{
"results" : {},
"status" : false,
}
Output with status 403 Forbidden
:
{
"code": 403,
"error":"API forbidden",
"status" : false
}
REQUIREMENTS
This library requires the following:
- PHP 7.4+
- CodeIgniter 4+
- agungsugiarto/codeigniter4-cors
- myth/auth
INSTALLATION
Run Composer in your Codeigniter project:
composer require meksiabdou/ci4-restful
CONFIGURATION
agungsugiarto/codeigniter4-cors
Config CORSTo allow CORS for all your routes, first register CorsFilter.php filter at the top of the $aliases property of App/Config/Filter.php class:
public $aliases = [
'cors' => \Fluent\Cors\Filters\CorsFilter::class,
// ...
];
Restrict routes based on their URI pattern by editing app/Config/Filters.php and adding them to the $filters array,
public filters = [
// ...
'cors' => ['after' => ['api/*']],
];
Config Public Token
To access public routes generate token and add $token_app
to app\Config\App.php
class App extends BaseConfig {
public $token_app = ['2ve7Wq9P2QLnzQMlN2uVnBfb10xvOY0NQTuQ7Q'];
//...
CI4Restful\Helpers\RestServer
,
Create a controller to extend class Store extends RestServer {}
Restful Auth API
https://yourname.com/api/login (POST)
https://yourname.com/api/register (POST)
https://yourname.com/api/logout (POST)
https://yourname.com/api/forgot-password (POST)
https://yourname.com/api/reset-password (POST)
https://yourname.com/api/update-user (PUT)
https://yourname.com/api/resend-activate-account (PUT)
https://yourname.com/api/p/update (for update password) (POST)
HttpRequest (public routes)
const formData = new FormData();
formData.append('identity', 'user@email.com')
formData.append('password', 'password123');
var requestOptions = {
method: 'POST',
headers: {
"token" : "2ve7Wq9P2QLnzQMlN2uVnBfb10xvOY0NQTuQ7Q"
},
body: formData,
redirect: 'follow'
};
fetch("https://yourname.com/api/login", requestOptions)
.then(response => response.json())
.then(result => console.log(result))
.catch(error => console.log('error', error));
HttpRequest (private route)
const formData = new FormData();
formData.append('email', 'user@email.com')
formData.append('password', 'password123');
formData.append('newPassword', 'newPassword123');
formData.append('confirmPassword', 'newPassword123');
var requestOptions = {
method: 'PUT',
headers: {
"token" : userToken,
},
body: formData,
redirect: 'follow'
};
fetch("https://yourname.com/p/update", requestOptions)
.then(response => response.json())
.then(result => console.log(result))
.catch(error => console.log('error', error));