sandip sandip - 1 year ago 127
reST (reStructuredText) Question

How to give string in the url in RESTful api in Cakephp?

As per the this Cakephp CookBook the simple setup for RESTful api :

HTTP Method URL.method Controller action invoked
GET /recipes*.method* RecipesController::index()
GET /recipes/123.method RecipesController::view(123)
POST /recipes*.method* RecipesController::add()
PUT /recipes/123*.method* RecipesController::edit(123)
DELETE /recipes/123.method RecipesController::delete(123)
POST /recipes/123*.method* RecipesController::edit(123)

here all the URL parameters are numeric i.e 123.
When I tried with the string i.e

GET /recipes/test.json RecipesController::view(123)

this gives me an error :

code: "404"
url: "/myproject/recipes/test.json"
name: "Action RecipesController::test() could not be found."

here the URL

"/myproject/recipes/test.json" // doesn't work

"/myproject/recipes/123.json" // works

I used the default

Thanks in advance!

Answer Source

Well, reading the API for that piece of code, the value passed before the dot is matched automatically to an id or UUID. Right in that API there's the params definitions

'id' - The regular expression fragment to use when matching IDs. By default, matches integer values and UUIDs.

What mapResources does is simply add a lot of Router::connect with some pre-established options (that basically has the form of :controller/:action/:id).

So, if the rule is to match ids (that cake considers to be ints) to a regular expression, clearly your string isn't passing that validation. So the Router skips that connect rule and goes to the other, until one matches. And the one that matches is in the form of :controller/:action.extension (probably). That's why you're getting that error, test is clearly not intended to be an action.

Luckily, one of that options that mapResourcesgives for customizing is the rule to match the $id.

To add the option of strings as "ids" (since that's the only variable the REST actions are going to receive if you add the connection routes with mapResources), change the regex that validates that rule like this

Router::mapResources('recipes', array('id'=>'[0-9A-Za-z]'));

or whatever rule you want to make (I'm bad with regex, so try to adjust it to what you need).

Take a look at the docs of the API to see what other options you can add.

Keep in mind that mapResources is there to make your life easier, so if you need more complicated routes with more params or something extra, consider forgetting about mapResources and constructing the routes yourself (like it says in the bottom of the page of the link you provided).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download