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条)

报告相同问题?

悬赏问题

  • ¥18 深度学习tensorflow1,ssdv1,coco数据集训练一个模型
  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题
  • ¥15 主机可以ping通路由器但是连不上网怎么办
  • ¥15 数据库一张以时间排好序的表中,找出多次相邻的那些行
  • ¥50 关于DynamoRIO处理多线程程序时候的问题
  • ¥15 kubeadm部署k8s出错
  • ¥15 Abaqus打不开cae文件怎么办?
  • ¥15 小程序准备上线,软件开发公司需要提供哪些资料给甲方