dougua9328
2015-10-11 06:06
浏览 78
已采纳

CodeIgniter + RESTful服务器在请求用户时抛出400响应


I'm working on tutorial Working with RESTful Services in CodeIgniter and i have problem with one line of code. In user_get() function condition if(!$this->get('id')) throws 400 response if i try to get resource http://localhostApp/api/user/1

What am I missing out?
Do i request resources wrong way?

This is my Api controller:

    class Api extends REST_Controller
    {
        function user_get(){
            if(!$this->get('id')){ //Problem line
                $this->response(NULL, 400);
            }
            $user = $this->user_model->get( $this->get('id') );
            if($user){
                $this->response($user, 200); // 200 being the HTTP response code
            }else{
                $this->response(NULL, 404);
            }
        }

        function user_post(){
            $result = $this->user_model->update( $this->post('id'), array(
                'name' => $this->post('name'),
                'email' => $this->post('email')
            ));

            if($result === FALSE){
                $this->response(array('status' => 'failed'));
            }else{
                $this->response(array('status' => 'success'));
            }

        }

        function users_get(){
            $users = $this->user_model->get_all();

            if($users){
                $this->response($users, 200);
            }else{
                $this->response(NULL, 404);
            }
        }
    }
    ?>

If you need any additional info, please let me know.
Thank you in advance

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douaoli5328 2015-10-11 06:45
    已采纳

    If you want to pass data to user_get(), You have to define it in your URL.

    http://localhostApp/api/user/1

    In here you define user. but there is no any id passed to call which user

    So your URL should be

    http://localhostApp/api/user/id/1

    Note: Don't Just copy and paste code from that site to your project. Change it to as your need

    打赏 评论
  • doujianzhi3358 2015-10-11 09:54

    This is how I have been using the RESTful service - using Phil Sturgeon Rest Server.

    Used POST instead of GET for retrieving the user details

    routes.php

    $route['api/view/user_details'] = 'api/view/user_details';
    

    Form for testing :

    <form class="form-horizontal inline" action="<?php echo base_url(); ?>api/view/user_details" method="post">
            <label for="user_id">Enter User ID</label>
            <input id="user_id" type="text" name="id" value="" />
            <input type="submit" name="submit" value="submit" />
    </form>
    

    Controller Method for returning the user information in JSON

    public function user_details_post()
    {
    
    
        $id = $this->post('id');
    
        $this->load->model('admin_model');
    
        $users = $this->admin_model->view_clients();
    
    
    
        // If the id parameter doesn't exist return all the users
    
        if ($id == NULL)
        {
            // Check if the users data store contains users (in case the database result returns NULL)
            if ($users)
            {
                // Set the response and exit
                $this->response(['all_users'=>$users], REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
            }
            else
            {
                // Set the response and exit
                $this->response([
                    'status' => FALSE,
                    'message' => 'No users were found'
                ], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
            }
        }
    
        // Find and return a single record for a particular user.
    
        $id = (int) $id;
    
        // Validate the id.
        if ($id <= 0)
        {
            // Invalid id, set the response and exit.
            $this->response(NULL, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
        }
    
        // Get the user from the array, using the id as key for retreival.
        // Usually a model is to be used for this.
    
        $user = NULL;
    
        if (!empty($users))
        {
            foreach ($users['user_details'] as $key => $value)
            {
                if (isset($value['id']) && $value['id'] === $id)
                {
                    $user = $value;
                }
            }
        }
    
        if (!empty($user))
        {
            $this->set_response([
                'user_details'=>$user], REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
        }
        else
        {
            $this->set_response([
                'status' => FALSE,
                'message' => 'User could not be found'
            ], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
        }
    
    }
    

    MODEL: Returning an array of users with all details to the controller

    public function view_clients()
        {
    
            $result=$this->db->get('clients');
    
            $results=array();
    
            foreach ($result->result() as $row)
            {
                $id = $row->id;
                $id = (int) $id;
                $results[] = array(
                    'id' => $id,
                    'full_name' => $row->full_name,
                    'gender' => $row->gender,
                    'email' => $row->email,
                    'dob' => $row->dob,
                    'phone' => $row->phone,
                    'address' => $row->address,
                    'username' => $row->username,
                    'country' => $row->country,
                    'city' => $row->city,
                    'rec_status' => $row->rec_status,
                    'crt_date' => $row->crt_date,
                    'email_ver_status' => $row->email_ver_status,
                    'mobile_ver_status' => $row->mobile_ver_status,
                );
                $return_array= array("status:" =>'success',
                    "user_details" => $results );
    
            }
    
            return($return_array);
        }
    

    Response am getting as JSON for the POST request with ID

    {
        "user_details": {
            "id": 1,
            "full_name": "sudheesh sudhakaran",
            "gender": "Male",
            "email": "mails2sudheesh@gmail.com",
            "dob": "12\/03\/1986",
            "phone": "0567462013",
            "address": "kottayil house po kundaliyoor
    thrissur",
            "username": "sudheesh",
            "country": "India",
            "city": "KERALA",
            "rec_status": "A",
            "crt_date": "",
            "email_ver_status": null,
            "mobile_ver_status": null
        }
    }
    

    To fetch the record of all users , POST a NULL value.

    The Questions asked while creating this are here

    打赏 评论

相关推荐 更多相似问题