Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Is there a way to hardcode REST route variables
  • I was wondering, I'm trying to clean up my rest api, Lets say I have two "objects" like users and photos and I want to have an api that allows users to comment on both these objects. An example of the route would be something like sending a post request to

    api.domain/comment/user/1234 with a query string parameter comment

    and a comment to a photo would be like 

    api.domain/comment/photo/1234

    it would be cool if I could just have a method in my rest controller like

         public function post_comment($type = "photo", $id = null){

         }

    where thats only routed if type == "photo"

    so basically the 2nd routing param is what type of object is being commented on, what I'm basically doing is having a publicly exposed method like


          public function post_comment($type = null, $id = null)
          {
                switch ($type) {
                      case "photo":
                            return $this->photo_comment($id, Input::get('comment'));
                            break;

                      case "user":
     return $this->user_comment($id, Input::get('comment'));

                            break;

                }
          }
        

    so I'm basically routing the input to private methods, it would be cool if i could skip this step some how
  • HarroHarro
    Accepted Answer
    I would do it the other way around, not have a 'comment' controller, but have a controller per object, and then use

    api.domain/photo/comment/1234 ( or api.domain/user/comment/1234 )

    so a photo and a user controller, both having a post_comment method, that receives the id as first parameter.

    And I would not use the query string, it's an update, so it should be a POST, not a GET.
  • Yeah I thought about doing that too, ill try it out. 

    As per your suggestion I've been trying to follow the design of other popular api's I wish there was a standardization. So for the above example it would be a POST request but some params are in a query string, OR you could even have the ID as in the form data and do it all that way, and now im seeing some api's where the incoming data is in a json string. 
  • I hardly ever use the query string, selection parameters (like your 1234) are URI segments, the rest is in the request payload, in JSON.

    A GET request is used for reading, a POST for updating. There is also PUT, PATCH and DELETE that you could use (not all webservers support these by default).

    The only place I sometimes use the query string is for stuff like column sort info, for example "/controller/index/16?sort=name&order=desc".
  • So would you still put the ID of the object in the URI segment or have it as a $_POST variable
  • I would put it in the URI for a GET, in the post body for a POST.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion