Search code examples
phpsymfonysymfony4symfony5php-7.4

Specifying custom data in Symfony Routing Annotation


I am using symfony 5, and specifying the routings as annotation. So now I have a requirement to pass some extra data along with the routes. It is not part of routing, but I need to have a value for each routing. So I am specifying the route as below, using the options object options={"label"="COMMMON_CLIENTMANAGEMENT"}.

It is not producing an error. So I am not sure if it is working or not. Also I have not been able to retrieve the data from any routing services.

 *   
 * @Route("/client/list", name="client_list", options={"label"="COMMMON_CLIENTMANAGEMENT"})
 * @return \Symfony\Component\HttpFoundation\Response
 */   

And I want to create an html based on this data, whick will be like <li href="{{path(route)}}">{{ label }}</li>


Solution

  • Update:

    So I did a bit more experimenting since I gather the intent is to use the 'label' data when listing routes. I think it is still easier to use the defaults section but you can access the options information using the route collection:

    class PlayCommand extends Command
    {
        protected static $defaultName = 'app:play';
    
        private RouterInterface $router;
    
        public function __construct(RouterInterface $router)
        {
            parent::__construct();
            $this->router = $router;
        }
        protected function execute(InputInterface $input, OutputInterface $output): int
        {
            $routes = $this->router->getRouteCollection();
            $route = $routes->get('index');
            $label = $route->getOption('label');
    

    Not sure if there is a way to directly get the route by from inside of twig but it would be easy enough to write a twig extension for this.

    Original Answer:

    The defaults section is used to provide additional information.

    I had a hard time finding the docs on the options section. This blog article talks about some new options such as utf-8 support. I think the options section is used by the router. Not positive.

        /**
         * @Route("/", name="index", options={"label"="COMMMON_CLIENTMANAGEMENT"})
         */