【问题标题】:Cake3 How to capture all characters in path property for rest routesCake3 如何捕获路径属性中的所有字符以用于休息路线
【发布时间】:2017-11-28 22:39:17
【问题描述】:

所以,我有一段代码,用于我的路线,如下所示:

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 成为您在上面的'delete' => ['action' => 'delete', 'method' => 'DELETE', 'path' => ':name'] 路由中看到的: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? 我不想这样做。

如何获取这个参数?

【问题讨论】:

    标签: php cakephp url-routing cakephp-3.0


    【解决方案1】:

    我也对地图有一些“乐趣”并放弃了它并这样做了:

    $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,
    ]);
    

    让我知道您是否清楚这个概念。如果没有,我会根据您的实际情况进行更改。

    【讨论】:

    • 看起来你必须通过一种超级复杂的方式自己做这个:\谢谢,我早上试试
    • 我同意,但试图通过地图完成它被证明是一个时间槽,所以我这样做了。写的更多,但没有浪费更多时间。
    猜你喜欢
    • 1970-01-01
    • 2019-06-29
    • 2019-12-16
    • 1970-01-01
    • 2016-05-09
    • 2012-06-12
    • 2016-06-04
    • 1970-01-01
    • 2015-04-19
    相关资源
    最近更新 更多