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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fastreport怎么判断当前页数
  • ¥15 Kylin-Desktop-V10-GFB-Release-JICAI_02- 2207-Build14-ARM64.iso有没有这个版本的系统啊
  • ¥15 能不能通过蓝牙将传感器数据传送到手机上
  • ¥20 100元python和数据科学实验项目
  • ¥15 根据时间在调用出列表
  • ¥15 R 包chipseeker 安装失败
  • ¥15 Veeam Backup & Replication 9.5 还原问题
  • ¥15 vue-print-nb
  • ¥15 winfrom的datagridview下拉框变成了黑色,渲染不成功
  • ¥20 利用ntfy实现短信推送