• logo
  • PHP
  • PHP OOPs
  • script
    • JavaScript
    • JQuery
    • Ajax
    • AngularJs
    • VueJs
    • NodeJs
    • ReactJs
  • wordpress
  • Codeigniter
  • Codeigniter 4
  • Laravel
  • Python
  • MySql
  • Json
  • C
  • C++
  • More ...
    • Cakephp Framework
    • MongoDb
    • MySqli PDO
    • .htaccess
    • HTML 5
    • CSS
    • SEO
    • DCA

Codeigniter 4 Tutorials

  • Codeigniter 4
  • Application Run
  • Application Structure
  • Configuration files
  • Errors
  • Controller
  • View
  • Model
  • How to remove index.php from URL
  • Route
  • CodeIgniter URLs
  • Helper
  • Session
  • Global Functions and Constants
  • Logging Information in CI4
  • Error Handling
  • Signin / SignUp
  • Login / Register
  • CRUD
  • Crud 1
  • Form Validation
  • Data Table
  • Pagination
  • File Upload
  • Show Data Using Ajax
  • Select2 AJAX Autocomplete Search
  • Curl Post
  • Rest API
  • Weblink
Home » Codeigniter 4  » 

How to create REST API in Codeigniter 4?


Create Table :-

File name : index.php

CREATE TABLE employees (
id int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary Key',
name varchar(100) NOT NULL COMMENT 'Name',
email varchar(255) NOT NULL COMMENT 'Email Address',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='datatable demo table' AUTO_INCREMENT=1;

INSERT INTO `employees` (`id`, `name`, `email`) VALUES
(1, 'Mahtab', 'mahtab@gmail.com'),
(2, 'Alam', 'alam@gmail.com'),
(3, 'Habib', 'habib@gmail.com'),
(4, 'Sana', 'sana@gmail.com'),
(5, 'Mahira', 'mahira@gmail.com'),
(6, 'Sara Kalam', 'sara@gmail.com'),
(7, 'Arham kalam', 'arham@gmail.com');

Model

File name : index.php

<?php
namespace App\Models;
use CodeIgniter\Model;

class EmployeeModel extends Model
{
protected $table = 'employees';
protected $primaryKey = 'id';
protected $allowedFields = ['name', 'email'];
}

REST Controller

File name : index.php

<?php namespace App\Controllers;
use CodeIgniter\RESTful\ResourceController;
use CodeIgniter\API\ResponseTrait;
use App\Models\EmployeeModel;


class Employee extends ResourceController
{
use ResponseTrait;

// all users
public function index(){
$model = new EmployeeModel();
$data['employees'] = $model->orderBy('id', 'DESC')->findAll();
return $this->respond($data);
}

// create
public function create() {
$model = new EmployeeModel();
$data = [
'name' => $this->request->getVar('name'),
'email' => $this->request->getVar('email'),
];
$model->insert($data);
$response = [
'status' => 201,
'error' => null,
'messages' => [
'success' => 'Employee created successfully'
]
];
return $this->respondCreated($response);
}

// single user
public function show($id = null){
$model = new EmployeeModel();
$data = $model->where('id', $id)->first();
if($data){
return $this->respond($data);
}else{
return $this->failNotFound('No employee found');
}
}

// update
public function update($id = null){
$model = new EmployeeModel();
$id = $this->request->getVar('id');
$data = [
'name' => $this->request->getVar('name'),
'email' => $this->request->getVar('email'),
];
$model->update($id, $data);
$response = [
'status' => 200,
'error' => null,
'messages' => [
'success' => 'Employee updated successfully'
]
];
return $this->respond($response);
}

// delete
public function delete($id = null){
$model = new EmployeeModel();
$data = $model->where('id', $id)->delete($id);
if($data){
$model->delete($id);
$response = [
'status' => 200,
'error' => null,
'messages' => [
'success' => 'Employee successfully deleted'
]
];
return $this->respondDeleted($response);
}else{
return $this->failNotFound('No employee found');
}
}

}

Route

File name : index.php

$routes->resource('employee');

Example:-

File name : index.php

https://mfikri.com/en/blog/codeigniter4-restful-api

File name : index.php


File name : index.php


File name : index.php


File name : index.php





Itechtuto

Connect Us Socially:

Quick Links

  • itech
  • About Us
  • Feedback
  • Trademarks
  • Privacy Policy
  • Terms of Use
  • Sitemap
  • Trademarks
  • Privacy Policy
  • Terms of Use
  • Sitemap

Copyright © 2016 itechxpert (P) Ltd. All rights reserved.

Copyright © 2016 itechxpert (P) Ltd. All rights reserved.