AtanuCSE
AtanuCSE

Reputation: 8940

How to route to two different 'Action' functions in same controller using annotation - Symfony2

Recently I shifted from Symfony 2.4 to Symfony 2.7

So I was following the new docs. Now say I have 2 action functions in same controller.

public function indexAction() {}

public function changeRateAction()

Previously I would have route them using routing.yml

change_interest_rate_label:
    path: /change_interest_rate
    defaults: { _controller: appBundle:appInterestRateChange:index }

change_interest_rate_action_label:
    path: /change_interest_rate_and_archived_action
    defaults: { _controller: appBundle:appInterestRateChange:changeRate }

Now in 2.7 docs, annotations are encourages. Inside controller file

/**
 * @Route("/home", name="homepage")
 */

This will fire the action method contains in the controller file. But how can I write annotations for 2 methods for different urls included in the same controller file ?

That means I have indexAction & changeRateAction in same controller file. I want to route url /home with index function and /change with changeRate function. How to do this using annotations ? I know how to do this using routing.yml

Upvotes: 0

Views: 1160

Answers (1)

Andrius
Andrius

Reputation: 5939

You use the annotation routing on a method, not a controller, really.

You just specify the route before every method. In your case it would be something like this:

/**
 * @Route("/home", name="homepage")
 */
public function indexAction() {
    ...
}

/**
 * @Route("/change", name="changerate")
 */
public function changeRateAction() {
    ...
}

Be sure to read more about routing in the documentation: http://symfony.com/doc/current/book/routing.html

Upvotes: 3

Related Questions