CakePHP REST路由不起作用 [英] CakePHP REST route does not work
问题描述
我正在尝试使用CakePHP构建一个restfull api,但是当我尝试通过url/api/categories/1.json获取记录时,我得到了消息:
I'm trying to build an restfull api with CakePHP, but when I try to get a record, via url /api/categories/1.json, I get the message:
{
"code": 404,
"name": "Action CategoriesController::api_1() could not be found.",
"message": "Action CategoriesController::api_1() could not be found.",
"url": "\/api\/categories\/1.json"
}
我想在使用api时使用前缀"api".当我使用url/api/categories.json时,很奇怪,然后它按预期方式工作,这意味着我得到了所有类别.
I want to use the prefix "api" for when I want to use the api. It's strange when I use the url /api/categories.json, then it works as expected, which means I get all categories.
这里有一些代码片段.
routes.php
Router::mapResources('categories');
Router::parseExtensions('json');
Router::resourceMap(array(
array('action' => 'index', 'method' => 'GET', 'id' => false, 'prefix' => 'api'),
array('action' => 'view', 'method' => 'GET', 'id' => true, 'prefix' => 'api'),
array('action' => 'add', 'method' => 'POST', 'id' => false, 'prefix' => 'api'),
array('action' => 'edit', 'method' => 'PUT', 'id' => true, 'prefix' => 'api'),
array('action' => 'delete', 'method' => 'DELETE', 'id' => true, 'prefix' => 'api'),
array('action' => 'update', 'method' => 'POST', 'id' => true, 'prefix' => 'api')
));
CategoriesController.php
class CategoriesController extends AppController {
public $components = array(
'RequestHandler'
);
public function api_index() {
$this->set('categories', $this->Category->find('all'));
}
public function api_view($id) {
$category = $this->Category->findById($id);
$this->set('category', $category);
}
}
core.php
Configure::write('Routing.prefixes', array('api'));
观看次数
我的视图在地图View/Categories/json/中,我将它们命名为api_index.ctp和api_view.ctp.
My views are within map View/Categories/json/ and I have named them api_index.ctp and api_view.ctp.
推荐答案
我通过为每个操作创建Router :: connect来解决此问题.但我不确定这是否是一个好的解决方案.
I fixed this issue by creating Router::connect for each action. But I'm not sure it is a good solution.
这是代码段.
router.php
Router::parseExtensions('json');
// index
Router::connect('/api/:controller',
array(
'prefix' => 'api',
'api' => true,
// 'controller' => 'categories',
'ext' => 'json',
'action' => 'index',
'[method]' => 'GET'
)
);
// view
Router::connect('/api/:controller/:id',
array(
'prefix' => 'api',
'api' => true,
// 'controller' => 'categories',
'ext' => 'json',
'action' => 'view',
'[method]' => 'GET'
)
);
// add
Router::connect('/api/:controller',
array(
'prefix' => 'api',
'api' => true,
// 'controller' => 'categories',
'ext' => 'json',
'action' => 'add',
'[method]' => 'POST'
)
);
// edit
Router::connect('/api/:controller/:id',
array(
'prefix' => 'api',
'api' => true,
// 'controller' => 'categories',
'ext' => 'json',
'action' => 'edit',
'[method]' => 'PUT'
)
);
// delete
Router::connect('/api/:controller/:id',
array(
'prefix' => 'api',
'api' => true,
// 'controller' => 'categories',
'ext' => 'json',
'action' => 'delete',
'[method]' => 'DELETE'
)
);
// update
Router::connect('/api/:controller/:id',
array(
'prefix' => 'api',
'api' => true,
// 'controller' => 'categories',
'ext' => 'json',
'action' => 'update',
'[method]' => 'POST'
)
);
core.php
Configure::write('Routing.prefixes', array('api'));
CategoriesController.php
class CategoriesController extends AppController {
public $components = array(
'RequestHandler'
);
public function api_index() {
$this->set('categories', $this->Category->find('all'));
}
public function api_view($id) {
$category = $this->Category->findById($id);
$this->set('category', $category);
}
}
这篇关于CakePHP REST路由不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!