所以,我有一段代码,对于我的路线,像这样:
Router::scope('/v1', function (RouteBuilder $routes) {
$routes->resources( 'Files', [
'map' => [
'index' => ['action' => 'index', 'method' => 'GET', 'path' => ''],
'create' => ['action' => 'add', 'method' => 'POST', 'path' => ''],
'view' => ['action' => 'view', 'method' => 'GET', 'path' => ':id'],
'update' => ['action' => 'edit', 'method' => ['PUT', 'PATCH'], 'path' => ':id'],
'delete' => ['action' => 'delete', 'method' => 'DELETE', 'path' => ':name'],
]
]);
});
我的网址如下:http://192.168.1.197/v1/files/13625a1ddedcbc2011-40115501.jpg
我希望13625a1ddedcbc2011-40115501.jpg
成为您在上面的:name
路线中看到的'delete' => ['action' => 'delete', 'method' => 'DELETE', 'path' => ':name']
参数,但无论我做什么(我甚至在这里尝试过正则表达式)我都会收到错误:
函数App \ Controller \ FilesController :: delete()的参数太少<0,传入
我已经阅读了相关的文档了一千次:https://book.cakephp.org/3.0/en/development/routing.html但我无法弄清楚如何。
我看了一下这个答案,对于如何使用它有点模糊,这意味着我将所有路由更改为:How to give string in the url in RESTful api in Cakephp?我不想这样做。
如何捕获此参数?
答案 0 :(得分:1)
我有一些&#34;有趣&#34;同时使用地图并放弃它并执行此操作:
$routes->connect('/:model/:foreignKey/:commentId', [
'plugin' => 'Burzum/Comments',
'controller' => 'Comments',
'action' => 'edit',
'_method' => 'PUT'
], [
'pass' => [
'model',
'foreignKey',
'commentId'
],
'_ext' => null,
'model' => $allowedModels,
'commentId' => $idRegex,
'foreignKey' => $idRegex,
]);
让我知道这个概念是否适合您。如果不是,我会根据您的实际情况进行更改。