如何在Cakephp的RESTful API中的URL中提供字符串? [英] How to give string in the url in RESTful api in Cakephp?

查看:124
本文介绍了如何在Cakephp的RESTful API中的URL中提供字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据CakePHP CookBook RESTful api的简单设置:

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)

此处所有URL参数均为数字,即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)

这给了我一个错误:

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

此处URL

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

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

我使用了默认的 Router :: mapResources('recipes' )

谢谢!

推荐答案

好,请阅读 API 代码,则在点之前传递的值会自动与 id UUID 匹配。在该API中,有参数定义

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’-匹配ID时使用的正则表达式片段。默认情况下,
匹配整数值和UUID。

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

什么 mapResources 只是简单地添加很多 Router :: connect 以及一些预先建立的选项(基本上是:controller /:action /:id )。

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).

因此,如果规则是将ID(该蛋糕认为是整数)与正则表达式进行匹配,则显然您的字符串没有通过该验证。因此,路由器会跳过该 connect 规则,然后转到另一个规则,直到一个匹配为止。匹配的格式为:controller /:action.extension (可能)。这就是为什么您会收到该错误的原因, test 显然不是要采取的措施。

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.

幸运的是, mapResources 自定义的选项之一就是匹配 $ id 的规则。

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

要将字符串选项添加为 ids(因为这是唯一的变量,如果您添加带有 mapResources ),更改像这样验证该规则的正则表达式

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).

看看 API的文档中查看您可以添加的其他选项。

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

请记住 mapResources 在那里使您的生活更轻松,因此,如果您需要带有更多参数或其他东西的更复杂的路线,请考虑忘记 mapResources 并自己构造路线(如它在您提供的链接页面的底部)。

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).

这篇关于如何在Cakephp的RESTful API中的URL中提供字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆