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

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?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试将RESTful服务集成到我的Codeigniter应用程序中。 我正在使用这个库 https://github.com/chriskacerguis/codeigniter-restserver 和 来自 https://code.tutsplus的教程 .COM /教程/工作用的RESTful服务功能于笨 - 网8814

但是,我对如何实现路由感到有点困惑。 本教程提到使用完整的URL,但我想做的事情如下:

我的控制器

< p> class AdminLogin_WS扩展了REST_Controller {

  public function __construct(){
 parent :: __ construct(); 
 $ this-&gt; load-&gt; model('  AccountModel'); 
} 
公共函数login_get(){
 $ this-&gt; response(json_encode(null)); 
} 
 
公共函数login_post(){
 $ username = $ this-&gt  ; post('username'); 
 $ this-&gt; response(json_encode($ username)); 
} 
   
 
 

} \ n

我的路线

$ route ['AdminLogin_WS / Login'] ['post'] ='AdminLogin_WS / login_post'; &lt; =这将触发未知方法错误

$ route ['AdminLogin_WS / Login'] ='AdminLogin_WS / login'; &lt; =这将调用get函数

REST请求

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

如何指定帖子或获取的功能?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

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

相关推荐 更多相似问题