[英]Configuring a route in Symfony with the same URL but different HTTP methods and controller actions
I have the following routes configuration in a Symfony application: 我在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 }
linked to executeConfigure
and executeCreate
actions. 链接到executeConfigure
和executeCreate
动作。 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
. 每当提交表单时,都会执行executeConfigure
,尽管据我所知,使用POST
方法配置的路由应避免这种情况并执行executeCreate
。
How can I differentiate between these two actions keeping the same URL? 如何区分保持相同URL的这两个操作?
Thanks! 谢谢!
I had this issue as well and found the answer in an old forum message (http://oldforum.symfony-project.org/index.php/t/25750/). 我也遇到了这个问题,并在旧的论坛消息(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. 如果它完全忽略了请求方法,则很可能使用常规的sfRoute。 You need to use sfRequestRoute to make the routing 'method-aware'. 您需要使用sfRequestRoute使路由“知道方法”。 So, in your example you will do: 因此,在您的示例中,您将执行以下操作:
label:
url: /label
class: sfRequestRoute
param: { module: label, action: configure }
requirements: { sf_method: get }
label_create:
url: /label
class: sfRequestRoute
param: { module: label, action: create }
requirements: { sf_method: post }
I solved it by using this routing sheme: 我通过使用以下路由sheme解决了它:
users_create:
pattern: /
defaults: { _controller: "RentalAPIBundle:User:create" }
requirements: { _method: post }
users:
pattern: /
defaults: { _controller: "RentalAPIBundle:User:index" }
requirements: { _method: get }
then when calling the Url you can either call user or user/ for GET but only users/ for POST. 那么在调用Url时,您可以调用user或user /进行GET,但只能调用users /进行POST。 I can't say why but it works 我不能说为什么,但是有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.