在yii \ web \ urlmanager配置中指定自定义操作的新休息路线的最可靠方法

发布于 2025-02-06 09:21:22 字数 1568 浏览 2 评论 0 原文

我已经创建了一个标准 yii \ yii \ rest \ rest \ activeController 并向它添加了我的自定义操作:

public function actionTest($id)
{
    return ['test' => $id];
}

为了使其正常工作,我已将新的条目添加到 extrapatterns of of yii \ yii \ web \ urlmanager

'extraPatterns' => [
    'GET test/{id}' => 'test',
],

已测试,都可以。然后,我想支持此路线的 post 动词,因此我添加了另一个条目:

'rules' => [
    [
        'class' => 'yii\rest\UrlRule',
        'controller' => 'user',
        'pluralize' => false,
        'except' => ['index'],
        'extraPatterns' => [
            'GET test/{id}' => 'test',
            'POST test/{id}' => 'test',
        ],
    ],

然后 delete SO 。而且...我确实做错了,因为:

  • 六种方法: post get put patch 选项 delete 每个自定义操作每个
  • 每个控制器五个不同的操作,
  • 七个REST控制器

我将最终将我的主配置文件生长到... 6 * 5 * 7 = 210行?

那么,YII 2中的最佳 /最可靠方法是确保每个方法 /动词都可以访问每个REST控制器中的每个自定义操作,而不是通过数百行将配置文件来制作此操作?

I have created a standard yii\rest\ActiveController and added my custom action to it:

public function actionTest($id)
{
    return ['test' => $id];
}

To make it work, I have added a new entry to extraPatterns of rules of yii\web\UrlManager:

'extraPatterns' => [
    'GET test/{id}' => 'test',
],

Tested, all OK. Then I wanted to support POST verb for this route, so I added another entry:

'rules' => [
    [
        'class' => 'yii\rest\UrlRule',
        'controller' => 'user',
        'pluralize' => false,
        'except' => ['index'],
        'extraPatterns' => [
            'GET test/{id}' => 'test',
            'POST test/{id}' => 'test',
        ],
    ],

Then DELETE and so on. And... there must be something that I am doing really wrong, because for:

  • Six methods: POST, GET, PUT, PATCH, OPTIONS and DELETE per each custom action
  • Five different actions per each controller and
  • Seven REST controllers

I'll end up with my main configuration file growing to... 6 * 5 * 7 = 210 lines?

So, what is in Yii 2 the best / the most reliable way of making sure that every custom action in every REST controller will be accessible for every method / verb and not making this by growing configuration file by hundreds of lines?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文