2016-05-23 127 views
0

我试图嵌套航线CakePHP的连接3CakePHP的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

再次仔细查看文档,only选项支持的值不包括addpatch值,除非您将具有这些名称的自定义路由添加到默认资源映射。

默认情况下,只有下列资源路由支持:

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

所以,你想用的是indexcreate为嵌套Users/Events资源路线,viewupdatedelete的非嵌套Events资源路线。

又见

+0

我彻底无缘路线名称。我想知道我从哪里得到这些路由名称。所以为了确保,'create'路由名称将连接到控制器中的'add'动作。与'update'和'edit'一样。补丁是一个完整的小姐在这里。 – Jan

+0

@Jan这是正确的 – ndm