Learn About API testing with REST Assured | Devstringx
Installation of Codeigniter 4
- Step1: Install the composer
- Step2: Install the Codeigniter 4
There are two ways to install the Codeigniter 4
Way 1 – create-project codeigniter4/app starter 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 a 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 a 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 APIs 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’);
Read Also:- How to Install Codeigniter 4
- Step10: past the below code in the 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”
Recommended to Read:- What Is Codeigniter and its Advantages?
How to use the custom helper in the Codeigniter?
Note: reference documentation
- Use of the model to communicate with the database
https://codeigniter.com/user_guide/models/model.html#finding-data
- How to use the Query builder?
https://codeigniter.com/user_guide/database/query_builder.html