I am using CodeIgniter 3 with the CodeIgniter REST Server library to build a RESTful API. I have a controller that extends REST_Controller.
I have noticed that CodeIgniter automatically routes HTTP requests (GET, POST, PUT, DELETE) to specific methods in the controller based on the HTTP request type. For example:
A GET request to /student triggers the index_get() method. A POST request to /student triggers the index_post() method. However, I couldn't find clear documentation that explains how this routing works under the hood. Does CodeIgniter map the HTTP methods (GET, POST, etc.) to methods in the controller automatically based on naming conventions like index_get(), index_post(), etc.?
More specifically:
Where in the CodeIgniter documentation or source code does it describe this behavior? Is there any official reference or explanation that confirms this automatic mapping of HTTP methods to controller methods? Example: Here is a sample controller code I am using:
class Student extends REST_Controller {
public function __construct() {
parent::__construct();
$this->load->model('api/Student_model');
}
// Handles HTTP GET requests (fetch student data)
public function index_get() {
// Code to fetch student data
}
// Handles HTTP POST requests (create new student)
public function index_post() {
// Code to insert student data
}
}
I would like to understand more about how this automatic routing works and if it is documented anywhere.
Thanks in advance!
EDIT 1
it can happen i was unable to make you guyz understand what i am asking, let me also share my real controller .
<?php
require APPPATH.'libraries/REST_Controller.php';
/**
* @property api/Student_model $Student_model;
*/
class StudentController extends REST_Controller
{
public function __construct()
{
parent::__construct();
//load database
$this->load->database();
$this->load->model(array("api/Student_model"));
}
public function index_post(){
//post method to insert data
echo "this is insert method";
}
public function index_put(){
//put methd to update data
echo "this is updates method";
}
public function index_delete(){
//delete method to delete data
echo "this is delete method";
}
public function index_get(){
//this method will bring data from db and list it
$students=$this->Student_model->get_students();
if(count($students)>0){
$this->response(array(
"status"=>"1",
"message"=>"students record found",
"data"=>$students
),REST_Controller::HTTP_OK);
}
else{
$this->response(array(
"status"=>"0",
"message"=>"No students record found",
"data"=>$students
),REST_Controller::HTTP_NOT_FOUND);
}
}
}//class closing
?>
so now i am running this using postman
GET REQUEST TO POSTMAN
http://localhost/cithreerestapi/index.php/api/StudentController
see, this link only have controller name , i am just asking how this link is able to conclude that which method it have to run or use in controlller file and it does it perfectly very well. In my code it runs method index_get
when i use GET
method in postman.
If i will use the same link and just change the http method to DELETE
in postman it runs index_delete
method in controller file. how? this is maybe somewhere built in or i am unable to research properly so i need your help team.
I am using CodeIgniter 3 with the CodeIgniter REST Server library to build a RESTful API. I have a controller that extends REST_Controller.
I have noticed that CodeIgniter automatically routes HTTP requests (GET, POST, PUT, DELETE) to specific methods in the controller based on the HTTP request type. For example:
A GET request to /student triggers the index_get() method. A POST request to /student triggers the index_post() method. However, I couldn't find clear documentation that explains how this routing works under the hood. Does CodeIgniter map the HTTP methods (GET, POST, etc.) to methods in the controller automatically based on naming conventions like index_get(), index_post(), etc.?
More specifically:
Where in the CodeIgniter documentation or source code does it describe this behavior? Is there any official reference or explanation that confirms this automatic mapping of HTTP methods to controller methods? Example: Here is a sample controller code I am using:
class Student extends REST_Controller {
public function __construct() {
parent::__construct();
$this->load->model('api/Student_model');
}
// Handles HTTP GET requests (fetch student data)
public function index_get() {
// Code to fetch student data
}
// Handles HTTP POST requests (create new student)
public function index_post() {
// Code to insert student data
}
}
I would like to understand more about how this automatic routing works and if it is documented anywhere.
Thanks in advance!
EDIT 1
it can happen i was unable to make you guyz understand what i am asking, let me also share my real controller .
<?php
require APPPATH.'libraries/REST_Controller.php';
/**
* @property api/Student_model $Student_model;
*/
class StudentController extends REST_Controller
{
public function __construct()
{
parent::__construct();
//load database
$this->load->database();
$this->load->model(array("api/Student_model"));
}
public function index_post(){
//post method to insert data
echo "this is insert method";
}
public function index_put(){
//put methd to update data
echo "this is updates method";
}
public function index_delete(){
//delete method to delete data
echo "this is delete method";
}
public function index_get(){
//this method will bring data from db and list it
$students=$this->Student_model->get_students();
if(count($students)>0){
$this->response(array(
"status"=>"1",
"message"=>"students record found",
"data"=>$students
),REST_Controller::HTTP_OK);
}
else{
$this->response(array(
"status"=>"0",
"message"=>"No students record found",
"data"=>$students
),REST_Controller::HTTP_NOT_FOUND);
}
}
}//class closing
?>
so now i am running this using postman
GET REQUEST TO POSTMAN
http://localhost/cithreerestapi/index.php/api/StudentController
see, this link only have controller name , i am just asking how this link is able to conclude that which method it have to run or use in controlller file and it does it perfectly very well. In my code it runs method index_get
when i use GET
method in postman.
If i will use the same link and just change the http method to DELETE
in postman it runs index_delete
method in controller file. how? this is maybe somewhere built in or i am unable to research properly so i need your help team.
- 1 As far as I know, there is no built-in mechanism that automatically maps HTTP verbs. It's CodeIgniter RestServer Library, maybe? – Abdulla Nilam Commented Jan 21 at 9:48
- i am using this rest api libary github.com/owthub/codeigniter-rest-api – aryan joshi Commented Jan 21 at 12:41
1 Answer
Reset to default 0According to the CI3 documentation, you can implement a _remap
function in your controller to override which method in the controller gets called, see: https://www.codeigniter.com/userguide3/general/controllers.html#remapping-method-calls
I'm not sure which CodeIgniter REST Server library you're using, but looking at this one https://github.com/transfuse/codeigniter-restserver/ , their REST_Controller
implements a _remap
function that concatenates the requested controller method ($object_called
) with the HTTP request method to create the new controller method to execute (line 665 in the libraries/REST_Controller.php
file), and then executes that (line 746).
Edit:
When receiving a request, CodeIgniter figures out what controller to use based on the requested url (or routing config file). It then checks if this controller has a _remap
method, and if it has one, it executes that instead of doing its default routing.
Because you're extending from REST_Controller
, it uses the _remap
method that is in there. The _remap
method receives for its argument the controller method name from the url, or index
if there is none. It adds an _
and the HTTP request method to that controller method name to determine the name of the controller method to call, and then calls that corresponding method on your controller.
Hope this clarifies it a bit more.