简体   繁体   English

Symfony:两种动作一条路线

[英]Symfony: Two actions one route

I have controller where I have two actions: createAction and showAction . 我有控制器,其中有两个动作: createActionshowAction

createAction creates form from form class and renders it to index.html.twig . createAction从表单类创建表单并将其呈现为index.html.twig

showAction makes database query and takes there some data and renders it to index.html.twig (same .twig file as before). showAction进行数据库查询,并在那里获取一些数据,并将其呈现到index.html.twig (与之前相同的.twig文件)。

How I can have two actions in one route? 如何在一条路线上执行两项操作? I tried to do two same routes but different name in routing.yml , but it doesn't work. 我尝试在routing.yml中执行两条相同的路由,但名称不同,但这是行不通的。 It only renders the first one. 它仅呈现第一个。

(Sorry, bad english) (对不起,英语不好)

You can have the same URL for two separate actions as long as they respond to different http verbs (POST/GET/PUT/etc). 您可以为两个单独的操作使用相同的URL,只要它们响应不同的http动词(POST / GET / PUT / etc)即可。 Otherwise how would you expect the router to decide which action to choose? 否则,您如何期望路由器决定选择哪种操作?

Learn how to define http method requirements from the Adding HTTP Method Requirements section of the Routing documentation. 从“路由”文档的“ 添加HTTP方法要求”部分了解如何定义http方法要求。

An example of annotation configuration: 注释配置的示例:

class GuestbookController
{
    /**
     * @Route("/guestbook")
     * @Method("POST")
     */
    public function createAction()
    {
    }

    /**
     * @Route("/guestbook")
     * @Method("GET")
     */
    public function showAction()
    {
    }
}

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

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