繁体   English   中英

在Symfony中使用相同的URL但使用不同的HTTP方法和控制器操作配置路由

[英]Configuring a route in Symfony with the same URL but different HTTP methods and controller actions

我在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 }

链接到executeConfigureexecuteCreate动作。 然后,我以这种方式配置了一个表单:

<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的这两个操作?

谢谢!

我也遇到了这个问题,并在旧的论坛消息(http://oldforum.symfony-project.org/index.php/t/25750/)中找到了答案。

如果它完全忽略了请求方法,则很可能使用常规的sfRoute。 您需要使用sfRequestRoute使路由“知道方法”。 因此,在您的示例中,您将执行以下操作:

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 }

我通过使用以下路由sheme解决了它:

users_create:
    pattern:     /
    defaults: { _controller: "RentalAPIBundle:User:create" }
    requirements: { _method: post }

users:    
    pattern:     /    
    defaults: { _controller: "RentalAPIBundle:User:index" }    
    requirements: { _method: get }

那么在调用Url时,您可以调用user或user /进行GET,但只能调用users /进行POST。 我不能说为什么,但是有效

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM