CodeIgniter Forums
REST_Controller does not work with parameters in the function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: REST_Controller does not work with parameters in the function (/showthread.php?tid=44141)



REST_Controller does not work with parameters in the function - El Forum - 08-04-2011

[eluser]xtcsonik[/eluser]
I have a custom route:
Code:
$route['users/(:num)/groups'] = 'groups/index/$1';
The controller looks like this:
Code:
require APPPATH.'/libraries/REST_Controller.php';

class Groups extends CI_Controller{

    public function index($user_id)
    {
            exit($user_id);
    }
}

It works fine when I'm using CI_Controller but when I switch it to REST_Controller and update the method to include the HTTP verb (index_get), I keep receiving an error:
Missing argument 1 for Groups::index_get(),

Any ideas?


REST_Controller does not work with parameters in the function - El Forum - 08-04-2011

[eluser]Andreas Karlsson[/eluser]
I don't know how the method is constructed but it wants an argument and you are not giving it one.

I have never tried the REST_Controller but you could try something like function index_get($argument = 'default var') where you declare the method.


REST_Controller does not work with parameters in the function - El Forum - 08-04-2011

[eluser]xtcsonik[/eluser]
Thanks for the response.

I'm starting to think its not possible to use Phil's rest server to create vanity urls. So urls like
'http://domain.com/api/users/381/friends?format=json'
are out of the question.

Instead we need to use
'http://domain.com/api/friends?user_id=381'


REST_Controller does not work with parameters in the function - El Forum - 08-04-2011

[eluser]Andreas Karlsson[/eluser]
I got interested in this and in my googleing i found this: http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/
Maybe it does help you, maybe not. You probably know this already Smile


REST_Controller does not work with parameters in the function - El Forum - 08-04-2011

[eluser]xtcsonik[/eluser]
Thanks. Have it pretty much memorized but haven’t been able to figure this one out.


REST_Controller does not work with parameters in the function - El Forum - 08-04-2011

[eluser]Phil Sturgeon[/eluser]
You could use GET variables as mentioned above and this was expected in older versions. I'm not sure when/if this made it into a tag yet, but this change was made here.

Try grabbing the latest file from GitHub to see if you can use URI segments properly there.

On the other hand it COULD be an issue with your routing. Did you try loading the URL directly instead of via the route?


REST_Controller does not work with parameters in the function - El Forum - 08-05-2011

[eluser]xtcsonik[/eluser]
Found and resolved. I grabbed the latest and it was fixed. I was using an older download package apparently.

Thanks for the help!