CakePHP 3在嵌套路由设置中缺少路由

时间:2016-05-23 19:02:14

标签: php rest cakephp url-routing cakephp-3.x

我正在尝试连接CakePHP 3中的嵌套路由。

我正在尝试实现以下路线(括号中的当前状态):

GET /api/users/:id/events     (Working)
POST /api/users/:id/events    (Missing Route)

GET /api/events/:id           (Working)
PATCH /api/events/:id         (Missing Route)
DELETE /api/events/:id        (Not tested)

在我的routes.php文件中,我有以下内容:

Router::prefix('api', function ($routes) {

    $routes->connect('/token', ['controller' => 'Users', 'action' => 'token']);

    $routes->resources('Users', function ($routes) {
        $routes->resources('Events', [
            'only' => ['index', 'add']
        ]);
    });

    $routes->resources('Events', [
        'only' => ['view', 'patch', 'delete']
    ]);
});

无效的路线会引发Cake\Routing\Exception\MissingRouteException

错误页面还会显示已连接路由的列表,而我想要的路由不存在。是否可以按照我尝试过的方式创建嵌套资源,或者如何在不手动连接每个路径的情况下连接所需的路由?

1 个答案:

答案 0 :(得分:1)

再次仔细查看文档,add选项不支持patchonly值,除非您添加带有这些名称的自定义路由默认资源图。

默认情况下,仅支持以下资源路由:

  • index(= GET
  • view(= GET/:id
  • create(= POST
  • update(= PUTPATCH /:id
  • delete(= DELETE/:id

因此,对于嵌套的index资源路由,您要使用createUsers/Eventsviewupdatedelete对于非嵌套的Events资源路由。

另见

相关问题