1

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?

JianYA
  • 2,750
  • 8
  • 60
  • 136

2 Answers2

2

You can specify is a function is post or get by using _post() and _get() respectively.

For your routing, I think you are routing wrongly. There should be a difference between the main route and the alternate route. You should have something like

$route['method/param1'] = 'controller/method/param1';
Sunday Okoi
  • 303
  • 2
  • 7
1

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?

Kdawg
  • 1,508
  • 13
  • 19
  • I'm using postman. If I don't create login_get() and only have login_post(), the route that I show above doesn't work – JianYA Jul 15 '17 at 14:46
  • But your `login_post()` does get called if you send a POST and you do have a `login_get` defined? If so, that sounds like odd behavior. I've never used the library that you're using, so I can't say that it definitely shouldn't work like that, but I wouldn't expect it. You might need to post your exact code to get more help from someone familiar with the library (I'm not), or possibly consider raising an issue on their Issues page. – Kdawg Jul 15 '17 at 15:04
  • It's strange. It keeps saying {"status":false,"error":"Unknown method"} with the post method. So for the post route I;ll use AdminLogin_WS/login_post and thatas the error that appears – JianYA Jul 15 '17 at 15:12
  • You're POSTing to `AdminLogin_WS/login_post`? I thought your route was `AdminLogin_WS/login`? If you POST to `AdminLogin_WS/login`, does it make it to your `login_post` function? – Kdawg Jul 15 '17 at 15:15
  • I'm posting to AdminLogin_WS/login_post. If i put that in my route, it will say unknown method. If I remove _post, the function called login_get will get triggered – JianYA Jul 15 '17 at 15:20
  • From the readme at https://github.com/chriskacerguis/codeigniter-restserver, it says: "When your controller extends from REST_Controller, the method names will be appended with the HTTP method used to access the request. If you're making an HTTP GET call to /books, for instance, it would call a Books#index_get() method. This allows you to implement a RESTful interface easily:" This makes me think that you wouldn't need to explicitly specify those routes, that the library takes care of that for you, and that you should be POSTing to `AdminLogin_WS/login` and not `AdminLogin_WS/login_post – Kdawg Jul 15 '17 at 15:34
  • Sorry I've updated my code with the request. How should I create a post request then? – JianYA Jul 15 '17 at 15:41
  • I haven't done a lot of PHP in a long time, and I'm not sure what you're using in that code example to interface with curl, but you might try https://stackoverflow.com/questions/28395/passing-post-values-with-curl. Since it's not totally clear which route you were POSTing to earlier, did you try POSTing to `AdminLogin_WS/login` (not `login_post`) from Postman or command line curl or some other third-party way of sending in a POST? If not, I'd try that. – Kdawg Jul 15 '17 at 16:01
  • I tried posting to AdminLogin_WS/login. I get the results from login_get. That is my current issue – JianYA Jul 15 '17 at 16:18
  • Just to check, how are you determining that you're getting the results from `login_get()`? It looks like in that method, you're just sending a null response, and you're not specifying a response in your `login_post()`, so maybe it's also returning a null response? Are you running in a debugger so you know it's making it to the `login_get()` and not `login_post()`? Have you tried printing anything out or setting a different response in your `login_post()`? I'm just really surprised that a presumably well-used library like that would send you to `login_get()` on a POST. – Kdawg Jul 15 '17 at 16:33
  • Sorry, updated the code again. I have included a response for the post. Previously I had set the login_get() to return something like "teststring" and when I accessed the url, it returned it. I use Postman to test and I also directly enter the url through the browser – JianYA Jul 15 '17 at 16:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/149294/discussion-between-kdawg-and-jianya). – Kdawg Jul 15 '17 at 16:40