为什么我不必为“ get”指定动词()对于我的自定义休息行动?
我已经创建了一个标准 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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在参考的问题/答案中,您正在使用现有操作
索引
,该已经具有yii \ filters \ derbfilter
仅允许get
get 代码>头部该动作的动词。但是,如果有全新的操作
测试
verbfilter
没有现有规则。如果没有操作规则,则动词
将允许任何请求。这就是为什么如果您不指定测试
操作路由器中设置的动词将起作用的操作。在test
中指定动词()
方法中的规则后,只允许动词将通过verbfilter
实现。In the referenced question/answer you were working with existing action
index
that already had rule foryii\filters\VerbFilter
to only allowGET
orHEAD
verbs for that action.But in case of completely new action
test
there is no existing rule forVerbFilter
. If there is no rule for action theVerbFilter
will allow any request. That's why if you don't specify rule fortest
action any verb set up in router will work. Once you specify the rule fortest
inverbs()
method, only allowed verbs will make it throughVerbFilter
.