CodeIgniter REST API Configuration Guide | Generated by AI

Home 2025.10

Below, I’ll provide a clear, step-by-step guide to understanding and using the CodeIgniter REST API configuration file you shared (application/config/rest.php). This file configures the behavior of the REST_Controller library (a popular PHP library for building RESTful APIs in CodeIgniter), such as Phil Sturgeon’s REST library (now maintained as part of the CodeIgniter framework in some versions).

I’ll explain:

If your question is specifically about one aspect (e.g., authentication or limits), feel free to clarify for more targeted advice!

1. Prerequisites and Setup

2. Key Configuration Options

I’ll summarize the main settings from your config file, grouped by purpose. These control global behavior. You can modify them to suit your needs (e.g., enable HTTPS or change default formats).

To modify: Edit rest.php and restart your app. Test changes carefully!

3. How to Use It: Step-by-Step Usage

Once set up, create API endpoints by building controllers that extend REST_Controller. Here’s a high-level process:

  1. Create a Controller:
    • In application/controllers/, create Api.php (or e.g., Users.php for a specific resource):
      <?php
      defined('BASEPATH') OR exit('No direct script access allowed');
      require_once(APPPATH . 'libraries/REST_Controller.php');
      
      class Api extends REST_Controller {
          public function __construct() {
              parent::__construct();
              // Optional: Set auth, limits per method
              $this->load->database();
              $this->method['index_get']['limit'] = 100; // 100 requests/hour
          }
      
          // GET /api
          public function index_get() {
              $data = ['message' => 'Welcome to the API', 'status' => 'success'];
              $this->response($data, REST_Controller::HTTP_OK);
          }
      
          // POST /api/users
          public function users_post() {
              $data = $this->input->post(); // Get POST data
              if (empty($data['name'])) {
                  $this->response(['error' => 'Name required'], REST_Controller::HTTP_BAD_REQUEST);
              }
              // Process (e.g., insert into DB)
              $this->response(['message' => 'User created'], REST_Controller::HTTP_CREATED);
          }
      
          // PUT /api/users/{id}
          public function users_put($id) {
              $data = $this->put(); // Get PUT data
              // Update user with $id
              $this->response(['message' => 'User updated'], REST_Controller::HTTP_OK);
          }
      
          // etc. for DELETE
      }
      
  2. Send Requests:
    • Use any HTTP client:
      • GET: curl http://yourdomain.com/api → Returns JSON {“message”: “Welcome to the API”, “status”: “success”}
      • POST: curl -X POST http://yourdomain.com/api/users -d "name=John" → Creates a user.
    • Include headers if using auth/keys (e.g., Authorization: Basic base64(user:pass) or X-API-KEY: yourkey).
  3. Test and Debug:
    • Check CodeIgniter logs for errors.
    • If auth fails, ensure credentials are set correctly.
    • For limits/logging, query your DB tables.

4. Common Tips and Best Practices

If this doesn’t answer your exact question or you need code for a specific use case (e.g., enabling OAuth), provide more details!


Back

x-ai/grok-code-fast-1

Donate