douji1058 2017-07-15 14:18
浏览 51
已采纳

Codeigniter RESTful服务路由

I am trying to integrate RESTful services to my Codeigniter application. I am using this library https://github.com/chriskacerguis/codeigniter-restserver and the tutorial from https://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter--net-8814.

However, I am a little confused about how to implement routing. The tutorial mentions using the full url but I'd like to do something like:

My Controller

class AdminLogin_WS extends REST_Controller {

public function __construct() {        
    parent::__construct();
    $this->load->model('AccountModel');
}
public function login_get(){
    $this->response(json_encode(null));
}

public function login_post(){
    $username = $this->post('username');
    $this->response(json_encode($username));
}

}

My routes

$route['AdminLogin_WS/Login']['post']= 'AdminLogin_WS/login_post'; <= this will trigger an unknown method error

$route['AdminLogin_WS/Login']= 'AdminLogin_WS/login'; <= this will call the get function

REST Request

public function ws_login(){
        $this->curl->create('https://url.com/AdminLogin_WS/Login');
        $this->curl->http_login('login','password');
        $this->curl->post(array(
            'username' => 'auser'
        ));
        $result = $this->curl->execute();
        var_dump(json_decode($result));
    }

How can I specify what function is a post or get?

  • 写回答

2条回答 默认 最新

  • dongluoqiu0255 2017-07-15 14:33
    关注

    Updated with information from chat

    Using login_get() and login_post() and then making the POST request to AdminLogin_WS/login was the correct thing to do, and the login_post() was getting called, there was just some confusion because the POST was returning the same response as the GET using the code that the poster was using.


    Original answer

    I would post this as a comment but don't have the rep to do so.

    What do you mean by "It only works if I create a controller function called login_get()"? That sounds to me like you're sending in a GET rather than a POST to your route. Can you give some information on how you're testing to see if you can POST and get to your login_post()? Have you tried downloading a tool like Postman (https://www.getpostman.com/) and sending in a POST to help eliminate the possibility that you're not sending in the POST correctly?

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

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料