为什么我不必为“ get”指定动词()对于我的自定义休息行动?

发布于 2025-02-06 16:31:00 字数 1464 浏览 2 评论 0原文

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

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

我已将新的条目添加到extrapatterns规则> of

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

lref =“ href =” https:// stackoverflow .com/a/72573743/1469208“>此答案,我还在我的rest控制器中添加了新的动词定义:

public function verbs()
{
    $verbs = parent::verbs();
    $verbs['test'] = ['GET'];
    return $verbs;
}

但是事实证明不需要。这意味着我可以评论上述代码(在这种情况下为整个方法),并且全新的休息路线仍在工作:

“在此处输入图像描述”

“

我缺少什么?为什么我不需要在 动词() 对于get(我想知道我必须为其他动词做这个)吗?

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

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

I have added a new entry to extraPatterns of rules of yii\web\UrlManager:

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

Following this answer, I have also added new verb definition in my REST controller:

public function verbs()
{
    $verbs = parent::verbs();
    $verbs['test'] = ['GET'];
    return $verbs;
}

But it turned out to be not needed. Meaning that I can comment out the above piece of code (the entire method in this case) and the whole new REST route is still working:

enter image description here

enter image description here

What am I missing? Why I don't need to specify this new action / router in verbs() for GET (I figured out that I must do this for other verbs)?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

本王不退位尔等都是臣 2025-02-13 16:31:00

在参考的问题/答案中,您正在使用现有操作索引,该已经具有yii \ filters \ derbfilter仅允许get get 代码>头部该动作的动词。

但是,如果有全新的操作测试 verbfilter没有现有规则。如果没有操作规则,则动词将允许任何请求。这就是为什么如果您不指定测试操作路由器中设置的动词将起作用的操作。在test中指定动词()方法中的规则后,只允许动词将通过verbfilter实现。

In the referenced question/answer you were working with existing action index that already had rule for yii\filters\VerbFilter to only allow GET or HEAD verbs for that action.

But in case of completely new action test there is no existing rule for VerbFilter. If there is no rule for action the VerbFilter will allow any request. That's why if you don't specify rule for test action any verb set up in router will work. Once you specify the rule for test in verbs() method, only allowed verbs will make it through VerbFilter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文