Symfony multiple route requirements from constants

Viewed 1102

I'm working on a Symfony 3.4 project, and I need to add a requirement on my route like :

/**
 * @Route("/{_locale}", defaults={"_locale"="en"}, requirements={
 *     "_locale"="en|fr"
 * })
 */
public function homepageAction($_locale)
{
    // ...
}

You can see "_locale"="en|fr", my problem is I need to get en and fr from a constant class not directly in the annotation.

So something like "_locale"="MY_FIRST_CONSTANTE|MY_SECOND_CONSTANTE"

I'm struggling with the syntax.

Thanks !

1 Answers

In your class where your constants are stored:

const EN = 'en'
const FR = 'fr'

const REQUIREMENTS = self::EN.'|'.self::FR;

Then, in your controller

/**
 * @Route("/{_locale}", defaults={"_locale"=App\Locales::EN}, requirements={
 *     "_locale"=App\Locales::REQUIREMENTS
 * })
 */
public function homepageAction($_locale)
{
    // ...
}
Related