PHP CODEIGNITER INSTALLATION GUIDE

Create APIs

Here is a basic example of a simple API for User Registration and Login.
Controller: application/controllers/Api.php
Copy code
defined('BASEPATH') OR exit('No direct script access allowed');
class Api extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('User_model');
header("Content-Type: application/json");
}
public function register() {
$data = json_decode(file_get_contents("php://input"), true);
$response = $this->User_model->register_user($data);
echo json_encode($response);
}
public function login() {
$data = json_decode(file_get_contents("php://input"), true);
$response = $this->User_model->login_user($data);
echo json_encode($response);
}
Model: application/models/User_model.php
Copy code
defined('BASEPATH') OR exit('No direct script access allowed');
class User_model extends CI_Model {
public function register_user($data) {
$data['password'] = md5($data['password']);
$this->db->insert('users', $data); return array('status' => true, 'message' => 'User registered successfully');
}
public function login_user($data) {
$this->db->where('email', $data['email']);
$this->db->where('password', md5($data['password']));
$query = $this->db->get('users');
if ($query->num_rows() > 0) {
return array('status' => true, 'message' => 'Login successful', 'data' => $query->row());
} else {
return array('status' => false, 'message' => 'Invalid credentials');
}
}
}