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"
}
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.
Here some code snippets.
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'));
Views
My views are within map View/Categories/json/ and I have named them api_index.ctp and api_view.ctp.