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
but
"/myproject/recipes/123.json" // works
I used the default Router::mapResources('recipes')
Thanks in advance!
Define in your routes below code :
Then create a controller for the API like below inside controller folder
How to pass parameters in API URL for XML format check below :-
How to pass parameters in API URL for JSON format check below :-
Well, reading the API for that piece of code, the value passed before the dot is matched automatically to an
id
orUUID
. Right in that API there's the params definitionsWhat
mapResources
does is simply add a lot ofRouter::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 thatconnect
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
mapResources
gives 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 thisor 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 aboutmapResources
and constructing the routes yourself (like it says in the bottom of the page of the link you provided).