CodeIgniter API中URL参数传递与获取方法咨询
Hey there! I’ve made the switch from raw PHP to CodeIgniter for APIs too, so I get where you’re coming from. Let’s tackle your two questions with clear code examples that fit CI’s workflow.
api.php?action=getCurrentOrder&grocery_id=1) CodeIgniter’s Input class makes this super straightforward—you don’t have to mess with raw $_GET (though you can, but using the Input class is safer and follows CI best practices).
Here’s a controller example to fetch those parameters:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Api extends CI_Controller { public function __construct() { parent::__construct(); // The Input library is auto-loaded by default, but adding this makes it explicit $this->load->library('input'); } public function handle_request() { // Fetch individual parameters (add TRUE to enable XSS filtering, recommended) $action = $this->input->get('action', TRUE); $grocery_id = $this->input->get('grocery_id', TRUE); // Or grab all GET parameters at once as an array $all_params = $this->input->get(); // Example response $response = [ 'status' => 'success', 'requested_action' => $action, 'grocery_id' => $grocery_id, 'all_received_params' => $all_params ]; // Set proper JSON header for API responses header('Content-Type: application/json'); echo json_encode($response); } }
You can access this via a URL like http://localhost/ci/index.php/Api/handle_request?action=getCurrentOrder&grocery_id=1—the Input class will pick up the query string parameters regardless of CI’s URL structure.
CodeIgniter supports two main ways to pass parameters, depending on whether you want to stick to your old query string style or adopt CI’s segmented URL approach.
Option 1: Segmented URLs (RESTful Style, Recommended)
CI’s default URL structure is controller/method/param1/param2/param3, which is cleaner for APIs. For your registration endpoint, the URL would look like:http://localhost/ci/index.php/UserApi/register/nitesh%20gupta/1/guptanitesh40@gmail.com/xxxxxyyyy
Here’s the controller code to match this:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class UserApi extends CI_Controller { public function register($name = '', $gender = '', $email = '', $mobile_number = '') { // First, validate that all required parameters are present if (empty($name) || empty($gender) || empty($email) || empty($mobile_number)) { $response = [ 'status' => 'error', 'message' => 'Missing required parameters' ]; header('Content-Type: application/json'); echo json_encode($response); return; } // Your registration logic here (e.g., save to database) // $this->load->model('User_model'); // $registration_success = $this->User_model->create_user($name, $gender, $email, $mobile_number); $response = [ 'status' => 'success', 'message' => 'User registered successfully', 'user_details' => [ 'name' => $name, 'gender' => $gender, 'email' => $email, 'mobile_number' => $mobile_number ] ]; header('Content-Type: application/json'); echo json_encode($response); } }
The parameters in the URL map directly to the method’s arguments in order. Setting default empty values prevents errors if a parameter is missing.
Option 2: Stick to Query Strings (Like Your Raw PHP Setup)
If you prefer keeping the ?key=value format, you just need to enable query strings in CI’s config:
- Open
application/config/config.php - Find
$config['enable_query_strings']and set it toTRUE
Then your registration URL can be:http://localhost/ci/index.php/UserApi/register?name=nitesh%20gupta&gender=1&email=guptanitesh40@gmail.com&mobile_number=xxxxxyyyy
And the controller code would use the Input class again:
public function register() { // Fetch each parameter with XSS filtering $name = $this->input->get('name', TRUE); $gender = $this->input->get('gender', TRUE); $email = $this->input->get('email', TRUE); $mobile_number = $this->input->get('mobile_number', TRUE); // Validate parameters if (empty($name) || empty($gender) || empty($email) || empty($mobile_number)) { $response = [ 'status' => 'error', 'message' => 'Missing required parameters' ]; header('Content-Type: application/json'); echo json_encode($response); return; } // Registration logic here... $response = [ 'status' => 'success', 'message' => 'User registered successfully', 'user_details' => [ 'name' => $name, 'gender' => $gender, 'email' => $email, 'mobile_number' => $mobile_number ] ]; header('Content-Type: application/json'); echo json_encode($response); }
Quick Tip
For production APIs, consider adding proper validation (CI has a Form Validation library that works great for API inputs) and error handling to make your endpoints more robust.
内容的提问来源于stack exchange,提问作者NITESH GUPTA

