在 Symfony 中配置具有相同 URL 但不同 HTTP 方法和控制器操作的路由
我在 Symfony 应用程序中有以下路由配置:
label:
url: /label
param: { module: label, action: configure }
requirements: { sf_method: get }
label_create:
url: /label
param: { module: label, action: create }
requirements: { sf_method: post }
链接到 executeConfigure
和 executeCreate
操作。然后我有一个这样配置的表单:
<form action="<?php echo url_for('@label_create') ?>" method="POST">
<?php echo $form->renderHiddenFields() ?>
<input type="hidden" name="sf_method" value="post" />
<!-- more stuff here -->
</form>
每当提交表单时都会执行 executeConfigure
,尽管据我所知,使用 POST
方法配置的路由应该避免这种情况并执行 <代码>executeCreate。
如何区分这两个保持相同 URL 的操作?
谢谢!
I have the following routes configuration in a Symfony application:
label:
url: /label
param: { module: label, action: configure }
requirements: { sf_method: get }
label_create:
url: /label
param: { module: label, action: create }
requirements: { sf_method: post }
linked to executeConfigure
and executeCreate
actions. Then I have a form configured this way:
<form action="<?php echo url_for('@label_create') ?>" method="POST">
<?php echo $form->renderHiddenFields() ?>
<input type="hidden" name="sf_method" value="post" />
<!-- more stuff here -->
</form>
Whenever the form is submitted executeConfigure
is executed, although as far as I know the route configured with POST
method should avoid that and executes executeCreate
.
How can I differentiate between these two actions keeping the same URL?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也遇到了这个问题,并在旧论坛消息中找到了答案(http://oldforum.symfony-project.org/index.php/t/25750/)。
如果它完全忽略请求方法,那么它很可能使用常规的 sfRoute。您需要使用 sfRequestRoute 使路由“方法感知”。因此,在您的示例中,您将执行以下操作:
I had this issue as well and found the answer in an old forum message (http://oldforum.symfony-project.org/index.php/t/25750/).
If it is completely ignoring the request method then it is most likely using the regular sfRoute. You need to use sfRequestRoute to make the routing 'method-aware'. So, in your example you will do:
我通过使用这个路由方案解决了这个问题:
然后在调用 Url 时,您可以调用 user 或 user/ 进行 GET,但只能调用 users/ 进行 POST。我不能说为什么,但它有效
I solved it by using this routing sheme:
then when calling the Url you can either call user or user/ for GET but only users/ for POST. I can't say why but it works