背景
我试图在cakephp中创建好的RESTful API,并将前端工作委托给前端模板语言Handlebars。如此处引用(http://www.dereuromark.de/2014/01/09/ajax-and-cakephp/),使用JsonView并制作扩展api是一种很好的方法。
问题:
$this->request->onlyAllow('ajax');
仅允许ajax是不允许返回的方法。我想强制执行此操作,因此我的API不会直接从浏览器调用。代码:
//At the controller
public function joinWithCode() {
//$this->request->onlyAllow('ajax'); //returns 405 method not allowed
//$this->request->onlyAllow('post'); //works and does not allow access from browser
$this->response->type('json');
$data = array(
'content' => 'something',
'error' => 'something else',
);
$this->set(compact('data'));
$this->set('_serialize', 'data');
}
//routes.php
Router::parseExtensions('json');
Router::connect('/Classrooms/join', array('controller' => 'classrooms', 'action' => 'joinWithCode'));
邮递员延期的帖子尝试: