CakePHP REST route does not work

2019-08-14 05:51发布

问题:

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.

回答1:

I fixed this issue by creating Router::connect for each action. But I'm not sure it is a good solution.

Here are the code snippets.

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);
    }
}