Reputation: 91
I have controller where I have two actions: createAction and showAction.
createAction creates form from form class and renders it to index.html.twig.
showAction makes database query and takes there some data and renders it to index.html.twig (same .twig file as before).
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. It only renders the first one.
(Sorry, bad english)
Upvotes: 1
Views: 2532
Reputation: 36241
You can have the same URL for two separate actions as long as they respond to different http verbs (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.
An example of annotation configuration:
class GuestbookController
{
/**
* @Route("/guestbook")
* @Method("POST")
*/
public function createAction()
{
}
/**
* @Route("/guestbook")
* @Method("GET")
*/
public function showAction()
{
}
}
Upvotes: 6