Codeigniter 4 Restful Api Crud Operation With JWT | Devstringx Technologies

Installation:-

Step1: Install the composer

Step2: Install the codeigniter 4

There are two way to install the codeigniter 4

Way 1:

create-project codeigniter4/appstarter codeigniter4POC

Way 2:

Visit the below link and download the codeigniter 4 structure

https://codeigniter.com/download

Step3: run composer update from the project root terminal

Step4: create token from your github account

Click on the link and generate the token from there.

Past the token in the terminal and press enter

Step4: start the server

Run command the “php spark serve”

Step5: create model “ php spark make:model post”

Step6: create table

“CREATE TABLE `codeigniter4poc`.`posts` ( `id` INT(50) NOT NULL AUTO_INCREMENT , `name` VARCHAR(100) NOT NULL , `created_at` TIMESTAMP NULL , `updated_at` TIMESTAMP NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;”

Step7: connect to database config/database.php

Step8: create controller by php spark make:controller Post — restful

Step9: create the routes for the api’s in config/routes.php

$routes->post(‘/getdata’, ‘PostCon::getdata’);

$routes->post(‘/insert’, ‘PostCon::insert’);

$routes->post(‘/updatedata’, ‘PostCon::updatedata’);

$routes->post(‘/deletePost’, ‘PostCon::deletePost’);

$routes->post(‘/passdatabyurl/(:any)(:any)’, ‘PostCon::passdatabyurl/$1/$2’);

$routes->post(‘/passdatabyformdata’, ‘PostCon::passdatabyformdata’);

$routes->post(‘/getdatabyrowjson’, ‘PostCon::getdatabyrowjson’);

Step10: past the below code in post controller

<?php

namespace App\Controllers;

use App\Models\Post;

use CodeIgniter\RESTful\ResourceController;

class PostCon extends ResourceController

{

/**

* Return an array of resource objects, themselves in array format

*

* @return mixed

*/

public function index()

{

$response_data=[

“error” => false,

“message” => “success”,

“status” => “Success”,

“status_code” => 200,

“data” => []

];

return $this->respond($response_data);

}

public function getdata()

{

try

{

$post = new Post();

$user = $post->findAll();

$response_data=[

“error” => false,

“message” => “success”,

“status” => “Success”,

“status_code” => 200,

“data” => $user

];

return $this->respond($response_data);

} catch (\Exception $e) {

$response_data=[

“error” => false,

“message” => $e->getMessage(),

“status” => “Success”,

“status_code” => 200,

“data” => $user

];

return $this->respond($response_data);

}

}

public function insert()

{

try

{

$post = new Post();

$data[‘name’] = “test”;

$post->save($data);

$response_data=[

“error” => false,

“message” => ‘Data inserted successfully’,

“status” => “Success”,

“status_code” => 200,

“data” => $data

];

return $this->respond($response_data);

} catch (\Exception $e) {

$response_data=[

“error” => false,

“message” => $e->getMessage(),

“status” => “Success”,

“status_code” => 200,

“data” => []

];

return $this->respond($response_data);

}

}

public function updatedata()

{

try

{

$post = new Post();

$post->set(‘name’, ‘tessssteing’);

$post->where(‘id’, 2);

$post->update();

$response_data=[

“error” => false,

“message” => ‘Data Updated successfully’,

“status” => “Success”,

“status_code” => 200,

“data” => []

];

return $this->respond($response_data);

} catch (\Exception $e) {

$response_data=[

“error” => false,

“message” => $e->getMessage(),

“status” => “Success”,

“status_code” => 200,

“data” => []

];

return $this->respond($response_data);

}

}

public function deletePost()

{

try

{

$post = new Post();

$post->where(‘id’, 1)->delete();

$response_data=[

“error” => false,

“message” => ‘Data deleted successfully’,

“status” => “Success”,

“status_code” => 200,

“data” => []

];

return $this->respond($response_data);

} catch (\Exception $e) {

$response_data=[

“error” => false,

“message” => $e->getMessage(),

“status” => “Success”,

“status_code” => 200,

“data” => []

];

return $this->respond($response_data);

}

}

public function passdatabyurl($num1,$num2)

{

//Get data from the url

echo $num1;

echo $num2;

die;

}

public function passdatabyformdata()

{

//get data from the form data

$data = $this->request->getPost();

print_r(json_decode(json_encode($data),true));

die;

}

public function getdatabyrowjson()

{

//get data from the json format

$data = $this->request->getJSON();

print_r(json_decode(json_encode($data),true));

die;

}

}

Step11: run the command from the root of the project

“php spark serve”

How to use the custom helper in the codeigniter

Step1:

Note: reference documentation

— use of model to communicate with database

https://codeigniter.com/user_guide/models/model.html#finding-data

— How to use the Query builder

.

.

Originally published at https://www.devstringx.com on Aug 21, 2021

Devstringx Technologies is highly recommended IT company for custom software development, mobile app development and automation testing services