CakePHP中的自定义REST路由 [英] Custom REST routes in CakePHP

查看:41
本文介绍了CakePHP中的自定义REST路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用CakePHP定义自定义的REST路由.我可以将路由创建为 post/user/:id .这是我的 routes.php 中的相关代码:

I want to define a custom REST route using CakePHP. I am able to create route as post/user/:id. Here is the related code in my routes.php:

Router::connect(
    "/pool/user/:id",
    array(
        '[method]' => 'DELETE',
        'controller' => 'user',
        'action' => 'delete',         
    )
);

现在,我想以 post/:id/user/:id 创建一条路由,其中​​第一个 id 是发布ID,第二个> id 是用户ID.但是当我这样做时,它给了我一个错误:

Now I want to create a route a route as post/:id/user/:id, where the first id is the post id and the second id is user id. But when I do so it gives me an error:

 preg_match(): Compilation failed: two named subpatterns have the same name at offset 39 [CORE/Cake/Routing/Route/CakeRoute.php, line 195]

如何创建所需的REST路由?

How do I create the desired REST route?

推荐答案

错误消息很清楚是什么错误,您不能有两个或更多个具有相同名称的命名元素.

The error message is pretty clear about what is wrong, you can't have two or more named elements with the same name.

解决方案应该很明显,为您的元素使用唯一的名称

The solution should be rather obvious, use unique names for your elements

post/:postId/user/:userId

这篇关于CakePHP中的自定义REST路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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