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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作