harpax
harpax

Reputation: 6106

CakePHP routing for a REST-Service

For the sake of an example i have user_group- and a user-Model and I would like to build a simple REST-API in Cake.

The structure of the app would be that there is an index-page for the user_groups, where I can do all the CRUD for the group. In the next step I would like to pick a group, see all corresponding users and be able to do all the CRUD things with them as well.

Now for the question: how to build the routes? It is straight forward for the user_groups, but I am not sure how to pass the selected user_group to the user-model (as a GET-param or as a different route (app/{user_group_name}/user/index), or ..)

I am using the Router::mapResource function (and Cake 2.5). Note that I am mainly trying out angularjs, so agreed, that the example is not so sophisticated

Upvotes: 0

Views: 91

Answers (1)

floriank
floriank

Reputation: 25698

Why aren't you simply using query params for that?

/users/index?group=14
/users/index?groupName=my-fancy-group

Routes will do it as well:

Router::connect(
    '/:groupName/:controller/:action/*',
    array(),
    array('pass' => 'groupName')
);

Passing the $groupName as first arg to the action.

Upvotes: 2

Related Questions