douji1058 2017-07-15 06: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 06: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条)
编辑
预览

报告相同问题?