【问题标题】:CakePHP REST route does not workCakePHP REST 路由不起作用
【发布时间】:2014-09-21 01:38:57
【问题描述】:

我正在尝试使用 CakePHP 构建一个 restfull api,但是当我尝试通过 url /api/categories/1.json 获取记录时,我收到消息:

{
"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 时很奇怪,然后它按预期工作,这意味着我得到了所有类别。

这里有一些代码sn-ps。

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。

【问题讨论】:

标签: php json rest cakephp


【解决方案1】:

我通过为每个操作创建 Router::connect 来解决此问题。但我不确定这是一个好的解决方案。

这里是代码sn-ps。

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

【讨论】:

  • 需要注意的一点:在 routes.php 中使用 Router::connect 定义所有路由确实解决了问题,但是对于最后包含 :id 的 API 路径,您需要指定第三个Router::connect() 中的参数。第三个参数必须是一个如下所示的数组:array('pass' => array('id'))。查看 CakePHP 文档以获取更多信息book.cakephp.org/2.0/en/development/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-03
  • 2014-07-15
  • 1970-01-01
相关资源
最近更新 更多