duanmu3049 2015-11-10 10:54
浏览 69

REST SERVER使用CodeIgniter 3获取函数

I'm currently studying rest api and i am using a ci framework with chris's rest server and i am having a problem with my users_get when i put the id in my links for example this,"http://localhost/api/example/users/id/1" it still shows all the data from the database, is my query wrong? here.

COntroller

public function users_get()
{
    // Users from a data store e.g. database
    $users = 
             $result=$this->regusers->get(); 


    $id = $this->get('result');

    // 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($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 as $key => $value)
        {
            if (isset($value['result']) && $value['result'] === $id)
            {
                $user = $value;
            }
        }
    }

    if (!empty($user))
    {
        $this->set_response($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

 <?php 

class Regusers Extends CI_Model{

public function get(){

    $query=$this->db->get('tbl_user');
    return $query->result();
}

}

View

<li><a href="<?php echo site_url('api/example/users'); ?>">Users</a> - defaulting to JSON</li>
        <li><a href="<?php echo site_url('api/example/users/format/csv'); ?>">Users</a> - get it in CSV</li>
        <li><a href="<?php echo site_url('api/example/users/id/1'); ?>">User #1</a> - defaulting to JSON  (users/id/1)</li>
        <li><a href="<?php echo site_url('api/example/users/1'); ?>">User #1</a> - defaulting to JSON  (users/1)</li>
        <li><a href="<?php echo site_url('api/example/users/id/1.xml'); ?>">User #1</a> - get it in XML (users/id/1.xml)</li>
        <li><a href="<?php echo site_url('api/example/users/id/1/format/xml'); ?>">User #1</a> - get it in XML (users/id/1/format/xml)</li>
        <li><a href="<?php echo site_url('api/example/users/id/1?format=xml'); ?>">User #1</a> - get it in XML (users/id/1?format=xml)</li>
        <li><a href="<?php echo site_url('api/example/users/1.xml'); ?>">User #1</a> - get it in XML (users/1.xml)</li>
        <li><a id="ajax" href="<?php echo site_url('api/example/users/format/json'); ?>">Users</a> - get it in JSON (AJAX request)</li>
        <li><a href="<?php echo site_url('api/example/users.html'); ?>">Users</a> - get it in HTML (users.html)</li>
        <li><a href="<?php echo site_url('api/example/users/format/html'); ?>">Users</a> - get it in HTML (users/format/html)</li>
        <li><a href="<?php echo site_url('api/example/users?format=html'); ?>">Users</a> - get it in HTML (users?format=html)</li>
  • 写回答

1条回答 默认 最新

  • douhanzhen8927 2015-11-11 15:47
    关注

    The controller will always return all users because $id is always FALSE or null due to the fact that there is no 'result' parameter in the the get request which you used to assign to the $id variable. I think the solution would be to change the URL to the following format api/example/users?id=1 then in controller use $id= $this->input->get("id"); to assign it to the variable.

    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统