CodeIgniter中REST API的GET/POST实现及报错解决求助
Hey there! Let's tackle your issues one by one—first fixing the immediate syntax error, then cleaning up other bugs, and finally walking through proper GET/POST implementation in CodeIgniter's REST API.
1. First: Fix the Syntax Error (ParseError)
The error you're seeing (unexpected '->' (T_OBJECT_OPERATOR), expecting ')') comes from line 15 of your Example_api.php controller. You can't pass $this->get('id') directly as a method parameter in PHP—method parameters need to be variable names, not dynamic expressions.
Corrected Controller Code
<?php require(APPPATH.'/libraries/REST_Controller.php'); class Example_api extends REST_Controller { // Critical fix: Extend REST_Controller, not CI_Controller! public function __construct(){ parent::__construct(); $this->load->model('user_model'); } // Fixed: Use a variable to fetch the ID inside the method function user_get() { $id = $this->get('id'); if(!$id) { $this->response(NULL, 400); // Bad request if no ID is provided } $user = $this->user_model->get_user($id); if($user) { $this->response($user, 200); // Success response } else { $this->response(NULL, 404); // User not found } } function users_get() { $users = $this->user_model->get_all(); if($users) { $this->response($users, 200); } else { $this->response(NULL, 404); } } }
2. Fix the Model Bug
There are two small issues in your User_model:
- A typo in
get_all()(you used$querybut defined the variable as$q) get_where()expects an associative array for the condition, not a raw ID value
Corrected Model Code
<?php class User_model extends CI_Model{ function __construct(){ parent::__construct(); } public function get_user($id){ // Fixed: Pass an associative array to target the 'id' column $q = $this->db->get_where("employee", ['id' => $id]); return $q->result_array(); } public function get_all(){ $q = $this->db->get("employee"); return $q->result_array(); // Fixed: Use $q instead of undefined $query } }
3. How GET Requests Work (Now Fixed!)
Your GET endpoints are ready to use:
GET /example_api/user?id=1: Fetches a single user by ID (returns 400 if no ID is provided, 404 if the user doesn't exist)GET /example_api/users: Fetches all users from theemployeetable
4. Implementing POST Requests
To add a POST endpoint (e.g., create a new user), add this method to your Example_api controller:
function user_post() { // Grab POST data from the request $data = [ 'name' => $this->post('name'), 'email' => $this->post('email'), // Add other columns from your 'employee' table here ]; // Validate required fields if(!$data['name'] || !$data['email']) { $this->response(['error' => 'Name and email are required fields'], 400); } // Insert the new user into the database if($this->user_model->create_user($data)) { // Fetch the newly created user to return in the response $new_user = $this->db->get_where('employee', ['email' => $data['email']])->row_array(); $this->response($new_user, 201); // 201 = "Created" HTTP status code } else { $this->response(['error' => 'Failed to create user'], 500); } }
Then add this method to your User_model:
public function create_user($data){ return $this->db->insert('employee', $data); }
Test this in Postman by sending a POST request to /example_api/user with a JSON or form-data body containing name and email.
5. Testing with Postman
After applying all fixes:
- For
GET /example_api/users: You'll get a 200 response with all users (or 404 if the table is empty) - For
GET /example_api/user?id=1: You'll get the user's data (or 404 if no user matches the ID) - For
POST /example_api/user: You'll get a 201 response with the new user's data if the request is valid
内容的提问来源于stack exchange,提问作者Tanwir Alam

