簡體   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