2017-07-15 14:18
浏览 46

Codeigniter RESTful服务路由

I am trying to integrate RESTful services to my Codeigniter application. I am using this library and the tutorial from

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() {        
public function login_get(){

public function login_post(){
    $username = $this->post('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(){
            'username' => 'auser'
        $result = $this->curl->execute();

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

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

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

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


< p> class AdminLogin_WS扩展了REST_Controller {

  public function __construct(){
 parent :: __ construct(); 
 $ this-&gt; load-&gt; model('  AccountModel'); 
 $ this-&gt; response(json_encode(null)); 
 $ 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函数


  public function ws_login(){
 $ this-&gt; curl  - &gt;创建(''); 
 $ this-&gt; curl-&gt; http_login('login','password'); 
 $ this-&gt; curl  - &gt; post(array(
 $ 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 ( and sending in a POST to help eliminate the possibility that you're not sending in the POST correctly?

    解决 无用
    打赏 举报

相关推荐 更多相似问题