Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using class constants in Symfony routing

I have a route defined with an annotation, like this:

/**
 * @Route("/doSomething/{param}", name="route_name", defaults={"param"=1})
*/

Is it possible to use a class constant instead of this hardcoded '1', like:

/**
 * @Route("/doSomething/{param}", name="route_name", defaults={"param"=MyBundle:MyEntity:DEFAULT_TYPE})
*/

(of course the above fails)

like image 746
Przemek Avatar asked Jan 16 '15 11:01

Przemek


1 Answers

Yes you can use constants in annotations, just use the FQN of the class:

/**
 * @Route("/doSomething/{param}", name="route_name", defaults={"param"=Namespace\MyBundle\MyEntity::DEFAULT_TYPE})
*/

If you are in the same namespace, or you import the class holding the constant, you can shorten it:

use Namespace\MyBundle\MyEntity;

/**
 * @Route("/doSomething/{param}", name="route_name", defaults={"param"=MyEntity::DEFAULT_TYPE})
*/
like image 93
Gerry Avatar answered Oct 21 '22 04:10

Gerry