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

发布于 2024-12-12 01:23:00 字数 858 浏览 0 评论 0原文

我在 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 的操作?

谢谢!

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尐偏执 2024-12-19 01:23:00

我也遇到了这个问题,并在旧论坛消息中找到了答案(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 }

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:

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 }
季末如歌 2024-12-19 01:23:00

我通过使用这个路由方案解决了这个问题:

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。我不能说为什么,但它有效

I solved it by using this routing 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. I can't say why but it works

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文