2016-05-23 46 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