Building RESTful APIs with CodeIgniter: A Complete Guide

CodeIgniter API with RESTful PHP elements

Introduction to Building RESTful APIs with CodeIgniter

CodeIgniter is a popular PHP framework used for building web applications. It provides a simple and efficient way to create RESTful APIs. In this guide, we will explore how to build a RESTful API using CodeIgniter. We will cover the basics of RESTful APIs, setting up CodeIgniter, and creating API endpoints.RESTful APIs are an architectural style for designing networked applications. They are based on the idea of resources, which are identified by URIs, and can be manipulated using a fixed set of operations. RESTful APIs are stateless, meaning that each request contains all the information necessary to complete the request.

Setting Up CodeIgniter

To get started with building a RESTful API using CodeIgniter, you need to set up the framework. You can download the latest version of CodeIgniter from the official website. Once you have downloaded the framework, extract it to your web server’s document root.Next, you need to configure the framework by opening the application/config/config.php file and setting the base_url to the URL of your application. You also need to set the index_page to an empty string, as we will be using a custom controller to handle API requests.

Creating API Endpoints

To create API endpoints, you need to create a new controller that will handle API requests. In CodeIgniter, controllers are classes that extend the CI_Controller class. For our API controller, we will create a new file called Api.php in the application/controllers directory.In the Api.php file, we will define a new class called Api that extends the CI_Controller class. We will also define several methods that will handle different types of API requests, such as GET, POST, PUT, and DELETE.

Handling GET Requests

To handle GET requests, we will create a new method called index in our Api controller. This method will retrieve data from the database and return it in JSON format.We will use the $this->input->get() method to retrieve any query parameters that were passed in the URL. We will then use these parameters to retrieve the relevant data from the database.

For example, if we have a table called users with columns id, name, and email, we can use the following code to retrieve all users:

$this->db->select('id, name, email');
$this->db->from('users');
$query = $this->db->get();
$data = $query->result_array();
echo json_encode($data);

Handling POST Requests

To handle POST requests, we will create a new method called create in our Api controller. This method will insert new data into the database.We will use the $this->input->post() method to retrieve any data that was passed in the request body. We will then use this data to insert a new record into the database.

For example, if we have a table called users with columns id, name, and email, we can use the following code to insert a new user:

$data = array(
  'name' => $this->input->post('name'),
  'email' => $this->input->post('email')
);
$this->db->insert('users', $data);
echo json_encode(array('message' => 'User created successfully'));

Handling PUT Requests

To handle PUT requests, we will create a new method called update in our Api controller. This method will update existing data in the database.We will use the $this->input->put() method to retrieve any data that was passed in the request body. We will then use this data to update the relevant record in the database.

For example, if we have a table called users with columns id, name, and email, we can use the following code to update a user:

$id = $this->input->put('id');
$data = array(
  'name' => $this->input->put('name'),
  'email' => $this->input->put('email')
);
$this->db->where('id', $id);
$this->db->update('users', $data);
echo json_encode(array('message' => 'User updated successfully'));

Handling DELETE Requests

To handle DELETE requests, we will create a new method called delete in our Api controller. This method will delete existing data from the database.We will use the $this->input->delete() method to retrieve any data that was passed in the request body. We will then use this data to delete the relevant record from the database.

For example, if we have a table called users with columns id, name, and email, we can use the following code to delete a user:

$id = $this->input->delete('id');
$this->db->where('id', $id);
$this->db->delete('users');
echo json_encode(array('message' => 'User deleted successfully'));

Conclusion

In this guide, we have learned how to build a RESTful API using CodeIgniter. We have covered the basics of RESTful APIs, setting up CodeIgniter, and creating API endpoints. We have also learned how to handle different types of API requests, such as GET, POST, PUT, and DELETE.By following this guide, you can create your own RESTful API using CodeIgniter. Remember to follow best practices, such as validating user input and handling errors, to ensure that your API is secure and reliable.

Scroll to Top