Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Default REST Function, like 'Index'?
  • Controller_Template has a default function route, 'index'. That is, whenever a URL does not specify the function explicitly, it defaults to 'index'. Here is the issue: let's say that I have a RESTful interface to access user information - I have a Controller_User based on Controller_Rest. Now I want to use the GET http method to retrieve all information from user with id '23'. At the moment, I have to use a URL like:
    http://myserver.com/user/func/23
    

    And in my controller I'd have "get_func" function. What I *want* to have is a cleaner URL, by not needing to specify the 'func' part:
    http://myserver.com/user/23
    

    In a RESTful interface, the URL should specify only the location of the resource in the internet, so cleaner URLs like this fit the bill better. Is there a default function like that for the methods in Controller_Rest - so that I don't have to include the 'func' name in the URL?
  • Have you looked at just creating a custom route for this? http://fuelphp.com/dev-docs/general/routing.html#/http_verb Example:
    return array(
    'user/(:num)' => array(array('GET', new Route('user/func/$1'))),
    );
  • Thank you for the suggestion. Yes, I had thought of that. At the moment, I'm achieving what I want at Controller level, with a magic route() function to the controller, like this:
    // The magic router() function automatically re-routes
     // all HTTP requests to the appropriate REST method.
     public function router($res, array $args){
      parent::router($res, $args);
      
      switch (Input::method()){
       case "GET":
        $this->get_index($res,$args);
        break;
       case "POST":
        $this->post_index($res,$args);
        break;
       case "PUT":
        $this->put_index($res,$args);
        break;
       case "DELETE":
        $this->delete_index($res,$args);
        break;
       default:
        // The request uses an unsupported http method
        // Handle the error here - i.e.:
        $data['error'] = "Unsupported HTTP method request.";
        $this->response($data);
      }
     }
    
    I wanted to see if there was something already 'built-in' in the framework. I noticed, for instance, that if no $resource is passed in the URI - ie., "http://server.com/monkey/"; - then the parent class does put "index" as the $res automatically. However, "http://server.com/monkey/33' does not automatically route to "get_index" or "post_index", etc., depending on the method. I need to specify "http://server.com/monkey/index/33"; instead of just 'http://server.com/monkey/33"... It seems the only way to get this going is by using custom routing, as shown here. Unless I'm missing something obvious?...
  • Very interesting function, what file has you placed this in? A write up explaining this would be awesome for the community =D
  • I'm having a similar problem. I read through the github issue, but it seems to have been dismissed. I don't think there's any problem with how FuelPHP works but I do think his post has some merit. For example if I have an article resource, to get a single article with ID of 10, I would use /article/10. However the default routing doesn't support this. Instead I would have to implement something like Controller_Article::get_article($article_id) and have a URI of /article/article/10. Alternatively, I could move the code from Controller_Article into something like Controller_Api and implement a get_article function. This would then allow me to use /api/article/10. However it gets more complex if I then want add another segment, such as api/article/10/author. With a lot of resources the controller will also become huge. I don't think using index methods like Knud suggested is the best option, since they would become too complex with lots of resources. I found a simpler solution which is to add a couple of routes:
    '(:segment)/(:num)' => '$1/item/$2',
    '(:segment)/(:num)/(:segment)' => '$1/$3/$2',
    

    This lets me route /article/10 to Controller_Article::get_item($article_id) and /article/10/author to Controller_Article::get_author($article_id). /article will still route to Controller_Article::get_index() All HTTP methods will still work and if a number doesn't follow the first segment then it will use the default behaviour.
  • @Aran: I've posted some code to: https://github.com/fuel/core/issues/649 It should be easy to follow.

Howdy, Stranger!

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

In this Discussion