![]() |
Controller Method file extensions? (.xml or .php) - 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: Controller Method file extensions? (.xml or .php) (/showthread.php?tid=18038) Pages:
1
2
|
Controller Method file extensions? (.xml or .php) - El Forum - 04-23-2009 [eluser]Fenix[/eluser] Hello, I'm building a RESTful API and would like some advice on how to accomplish user-defined return types the following way: When accessing a particular endpoint of the API (in this example, the "categories" endpoint) the user would be able to specify the return type by adding the desired file extension like this: Code: // For an XML response I have the query string part figured out, and I'd prefer to do the response type this way rather than having the it as an item in the query string. So, it's not possible to have a method in my API controller be categories.xml() so how can this be done in a fairly simple way? I assume there is a dynamic URL routing method? Controller Method file extensions? (.xml or .php) - El Forum - 04-23-2009 [eluser]slowgary[/eluser] You could use the _remap() method and break the URI up yourself. Something like this: Code: function _remap($method) I haven't tested that so pardon me if you try it and it blows up the whole world. Also, this could be expensive, I'm not really sure. Controller Method file extensions? (.xml or .php) - El Forum - 04-24-2009 [eluser]xwero[/eluser] I wonder why you need to mix segments with query strings they make it more difficult for the user to create the url. I would allow users to create urls like these Code: // For an XML response So the controller method would look like this Code: function categories() Controller Method file extensions? (.xml or .php) - El Forum - 04-24-2009 [eluser]Fenix[/eluser] On the contrary. I find it much easier to use the URLs the way I am doing it now: Code: // to get the CI part of the URI (/api/categories) I just do this: I'm going to try the _remap() method as slowgary descibed above. I'll update with my results. THanks so far! Controller Method file extensions? (.xml or .php) - El Forum - 04-24-2009 [eluser]xwero[/eluser] wasn't the point to identify the format? I don't see it in your latest snippet. Controller Method file extensions? (.xml or .php) - El Forum - 04-24-2009 [eluser]slowgary[/eluser] He means the user that will be making the api calls. I think the user will figure it out. Controller Method file extensions? (.xml or .php) - El Forum - 04-24-2009 [eluser]Fenix[/eluser] I haven't tried the remap method yet, but this is what I have it doing now. If a user wants a response type other than the default of XML, they would pass an argument "response" with "php". I only posted the last snippet because you said you thought query strings make it more difficult. I was showing the contrary. Code: if(array_key_exists('response',$this->query_string)) Controller Method file extensions? (.xml or .php) - El Forum - 04-24-2009 [eluser]xwero[/eluser] [quote author="Fenix" date="1240599325"]I only posted the last snippet because you said you thought query strings make it more difficult. I was showing the contrary.[/quote] Query strings make it difficult for the user to build their custom data request. Isn't http://mysite.com/api/categories/orderby/id/sort/desc.xml?accesskey=555555555 easier to understand than http://mysite.com/api/categories.xml?accesskey=555555555&orderby=id&sort=desc Functionalities should always be developed thinking about the users first. For example quite a few people don't like it the database api uses so many methods to build a sql statement. I think they rather saw only three methods for the select statement: rows, row and value. And these statement have all the parameters to build any kind of sql statement. The same goes for building urls. You have a part as segments and a part as a query string separated by the accesskey. of course it's up to you if you want to make it easy on yourself or on your site/app users. Controller Method file extensions? (.xml or .php) - El Forum - 04-24-2009 [eluser]Fenix[/eluser] I'm working with the _remap() method recommended by slowgary above, it's working great for the xml type but when I get to the php type, CI is assuming I'm looking for a php file and gives me a 404 Not Found error. Any way I can get around this? Here is what I have so far. It doesn't actually remap anything but it gets the content type for the response at least for xml. Code: function _remap($method) Controller Method file extensions? (.xml or .php) - El Forum - 04-24-2009 [eluser]Fenix[/eluser] [quote author="xwero" date="1240601162"] Query strings make it difficult for the user to build their custom data request. Isn't http://mysite.com/api/categories/orderby/id/sort/desc.xml?accesskey=555555555 easier to understand than http://mysite.com/api/categories.xml?accesskey=555555555&orderby=id&sort=desc [/quote] I think you are contradicting yourself with this one. URLs are hierarchical. It is much easier for the user to see the key-value pairs in the query string. It also eliminates confusion when you get into required vs optional parameters. I also believe you lose site of the actual endpoint you are trying to access if you use your method. I am not completely abandoning your method however. There are circumstances when appropriate data will be passed before the query string. Example: api/user/{user name}/items?from_date=123456&to_date=123556&sort=desc Moreover, this is how many, if not most, REST APIs work. Digg API Last.fm API Flickr API |