如何使用相同的路径但不同的 http 方法创建 2 个动作 Symfony2 [英] How to create 2 actions with same path but different http methods Symfony2

查看:27
本文介绍了如何使用相同的路径但不同的 http 方法创建 2 个动作 Symfony2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 symfony 路由注释并且我已经将 http_method_override 设置为 true

I work with symfony routing annotation and I have already set the http_method_override to true

我想根据 http 方法创建两个不同的动作,但具有不同的行为,如下所示:

I want to create two different actions but with a different behaviour according to the http method like this:

/**
 * Event controller.
 *
 * @Route("/event")
 */
class EventController extends Controller
{

    /**
     * Lists all Event entities.
     *
     * @Route("/", name="event")
     * @Method("GET")
     * @Template() // default template (index.html.twig)
     */
    public function indexAction()
    {
       ...
    }

    /**
     * Creates a new Event entity.
     *
     * @Route("/", name="event_create")
     * @Method("POST")
     * @Template("...") // a special template new.html.twig
     */
    public function createAction(Request $request)
    {
        ...
    }

但是当我尝试访问/event/时,有一个 405 页面说:

But when I try to access to /event/, there is a 405 page saying :

未找到GET/event/"的路由:方法不允许(允许:POST)

No route found for "GET /event/": Method Not Allowed (Allow: POST)

当我尝试使用 php app/console router:debug 列出我的路由时:

And when I try to list my routes with php app/console router:debug :

  event_create             POST   ANY    ANY  /event/                                      
  event                    GET    ANY    ANY  /event/week/{timestamp}                      
  event_new                GET    ANY    ANY  /event/new
  event_show               GET    ANY    ANY  /event/{id}                                  
  event_edit               GET    ANY    ANY  /event/{id}/edit
  event_update             PUT    ANY    ANY  /event/{id}
  event_delete             DELETE ANY    ANY  /event/{id} 

推荐答案

对不起,我刚刚意识到阅读我的问题 下面的其他方法,路径为/event/week/{timestamp} 也被命名为事件" :/

I'm sorry, I've just realized reading my question that an other method below with the path /event/week/{timestamp} was named "event" too :/

所以我将此方法重命名为 event_week 并且它有效.

So I renamed this method to event_week and it works.

这篇关于如何使用相同的路径但不同的 http 方法创建 2 个动作 Symfony2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆