问题描述
我code客户端/服务器应用程序。
I code a client/server application.
- 服务器端被CakePHP的2.4.7供电。
- 客户端运行在移动设备上angularjs和科尔多瓦
我用几个CakePHP的路径prefixes的管理员和移动。
(我使用$资源和$ httpInterceptor angularjs工厂设置我的REST请求。)
I use several cakephp route prefixes whose 'admin' and 'mobile'.(And I use $resource and $httpInterceptor angularjs factories to setup my REST requests.)
我要打电话 /website/mobile/posts/add.json
与JSON数据发布。
I want to call /website/mobile/posts/add.json
with json data posting.
所以我叫 /website/mobile/posts.json
用POST阿贾克斯查询:
So I call /website/mobile/posts.json
with a POST ajax query:
问题是在这里:被叫控制器动作是索引,而不是添加;
The problem is here: the called controller action is 'index', not 'add';
在我的PostsController的上方,我加了这一点:
On top of my PostsController, I added this:
echo $this->request->params['action'] . "\n";
echo ($this->request->isPost())? "post" . "\n" : "not post";
die;
和反应永远是:
mobile_index
post
所以Ajax请求似乎是正确的,但CakePHP的不将其映射到加行动,但指标之一。
但是我的配置似乎也不错;这里是我的routes.php文件片段
So the ajax request seems correct but cakephp don't map it to the add action, but index one.However my configuration seems good too; here is a fragment of my routes.php
Router::mapResources(array('users','posts'));
Router::parseExtensions('json');
任何想法?
推荐答案
REST自动创建传递给控制器路由器::马presources路线路由作品()
。 preFIX路由pretty多不相同,它创建默认路由,包括路由。prefixes定义的prefixes
。
Prefix routing doesn't work with REST routing out of the box
REST routing works by automatically creating routes for the controllers passed to Router::mapResources()
. Prefix routing pretty much does the same, it creates default routes including the prefixes defined in Routing.prefixes
.
不过两者功能不知道对方,他们都创造separte路线,所以路由器::马presources()
将连接到URL,而不prefixes(这种方法的 preFIX
选项的不的使用实际preFIX路由,它只会增加值该选项的URL的开头来连接!),因此您的要求 /移动/...
实际上并未使用REST路由,但只有preFIX路由。
However both functionalities don't know about each other, they both create separte routes, so Router::mapResources()
will connect to URLs without prefixes (the prefix
option for this method is not using actual prefix routing, it will just add the value of that option to the beginning of the URL to connect to!), and therefore your request to /mobile/...
doesn't actually use REST routing but only prefix routing.
有对于像使用选项或东西,而不是你必须手动定义REST路由,使preFIX选项包括,够简单,虽然这个问题没有简单的解决方法。
There is no simple fix for this problem like using an option or something, instead you'll have to define the REST routes manually so that the prefix option is included, simple enough though.
请参阅和。
一个样品路由看起来是这样的:
A sample Route could look like this:
Router::connect(
'/mobile/users',
array(
'prefix' => 'mobile',
'mobile' => true,
'controller' => 'users',
'action' => 'add',
'[method]' => 'POST'
)
);
这将POST请求连接到 /移动/用户
到 UsersController :: mobile_add()
。类似地,你必须对所有其他的方法,如 GET
和 PUT
,有和没有传递一个<$做到这一点C $ C> ID 等
This would connect POST requests to /mobile/users
to UsersController::mobile_add()
. Similary you'll have to do this for all other methods like GET
and PUT
, with and without passing an id
, etc.
注意,手动连接时,你可以沟路由。prefixes
选项,当然还有调用路由器::马presources()
。
Note that when connecting manually you can ditch the Routing.prefixes
option and of course the call to Router::mapResources()
.
手工定义所有这些路线是有点辛苦,你就到的。
Defining all those routes by hand is kinda exhausting, you're better of automating it with respect to the Router::resourceMap()
configuration.
下面是关于如何做到这一点的例子,它有点类似于路由器::马presources()
,但它接受 preFIX
选项,实际利用preFIX路由:
Here's an example on how to do that, it's somewhat similar to Router::mapResources()
, but it accepts a prefix
option that actually makes use of prefix routing:
function mapResources(array $controllers) {
$resourceMap = Router::resourceMap();
foreach($controllers as $controller => $options) {
if(!is_array($options)) {
$controller = $options;
$options = array();
}
$options += array(
'prefix' => null,
'plugin' => null,
'id' => Router::ID . '|' . Router::UUID
);
foreach($resourceMap as $params) {
$url = '';
if($options['prefix']) {
$url .= '/' . $options['prefix'];
}
if($options['plugin']) {
$url .= '/' . $options['plugin'];
}
$url .= '/' . $controller;
if($params['id']) {
$url .= '/:id';
}
Router::connect(
$url,
array(
'prefix' => $options['prefix'],
$options['prefix'] => !!$options['prefix'],
'plugin' => $options['plugin'],
'controller' => $controller,
'action' => $params['action'],
'[method]' => $params['method']
),
array(
'id' => $options['id'],
'pass' => array('id')
)
);
}
}
}
您会这样称呼它:
mapResources(array(
'books' => array(
'prefix' => 'mobile'
)
));
和它都会为你的图书REST路线图
使用控制器移动
preFIX。
and it would map all the REST routes for your books
controller using the mobile
prefix.
这篇关于如何在CakePHP中使用prefixes连接后休息请求添加控制器动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!