symfonyelfinder

Symfony, route conflicts between controller and bundle


For example simple controller:

    /**
     * @Route("/{identifier}", name="page")
     */
    public function page(Request $request, string $identifier)
    {
        $page = $this->pageRepository->findOneBy(['identifier' => $identifier]);

        if (!$page || !$page->getEnabled()) {
            throw $this->createNotFoundException();
        }
        
         return $this->render('cms/index.html.twig', []);
     }

And a have a bundle to manage images from admin page elfinder, which will enter the /elfinder link.

And instead of getting the bundle controller, my controller gets.

/{identifier} === /elfinder

How do people usually act in such situations?

I tried to set different priority, but it does not help


Solution

  • Try adding your controllers with the priority required in the annotations.yaml file. Thus, if you get a 404 in the first one, Symfony will try to open the route from the next controller

    Add your controllers to config/routes/annotations.yaml

    page:
        resource: App\Controller\_YourFistController_
        type: annotation
    
    elfinder:
        resource: FM\ElfinderBundle\Controller\ElFinderController
        type: annotation
    

    Or if this option does not suit you, then you can try the optional parameter priority. symfony doc

    Add to config file config/routes.yaml:

    elfinder:
        path: /elfinder/{instance}/{homeFolder}
        priority: 2
        controller: FM\ElfinderBundle\Controller\ElFinderController::show