Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
get_index($id) doesn't work in Controller_Rest
  • I m trying to get data from :

    /contents/4.json 

    With the following controller... But it doesn't work..  

    class Controller_Contents extends Controller_Rest
    {

    public function get_list($id)
    {

    //contents/list/3  WORKS
    return $this->response("test".$id);
    }
    public function get_index($id)
    {
    //contents/3 Doesn't WORK
    return $this->response("test".$id);
    }
    }

  • Why do you think "/contents/4.json" would call get_index?

    Without a route in place, the route will be automatically determined, where the first segment is the controller, the second segment the method, and additional segments are parameters. Or, in the case of a module, the first segment is the module, and everything moves one position.

    So, with this URI, the framework tries to find Controller_Contents::get_4(). Which does not exist.

    The search will only default to the 'index' method is there is no second segment. Which is not the case here.
  • So, with cakePHP , this features works.  controller/index(parms)  means conroller/params.
    That's why I was thinking that.

    So, I have to create a route to make it works... Thanks 
  • I find that dangerous, because that means you'll route all unknown method requests to 'index' too, as the router doesn't know what is and isn't a valid param.

    By having to define a route for it, you explicitly define that this is the behaviour your want.

    Alternatively, you can add a router() method to the controller. If it exists, it will be called no matter what method was requested. In it, check if the method is a defined action. If so, simply return the call to that action. If not, assume it's a parameter (or validate it if needed) and call the action_index with it.

    You can have a peek in the Controller_Rest base class, which does something similar.

Howdy, Stranger!

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

In this Discussion