Search code examples
phpruby-on-railssymfonysonata-adminsymfony-sonata

Symfony route being lost when importing multiple yaml files


I am setting up an admin for two entities in my system ("Link" and "Contact"), however I have a problem when I import a second admin.yml file. This is my working config.yml:

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: @AyrshireMinisContactBundle/Resources/config/admin.yml }

However, when I add this line just below I lose all the routing attached to the AyrshireMinisContactBundle and get a myriad of errors thrown at me (essentially saying that the rout for the Contact bundle cannot be found):

- { resource: @AyrshireMinisCommonBundle/Resources/config/admin.yml }

How can I configure my config.yml to allow for both admin files to be pulled in for these entities?

The error:

CRITICAL - Uncaught PHP Exception Twig_Error_Runtime: "An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "admin_ayrshireminis_contact_contact_create" as such route does not exist.") in "SonataAdminBundle:Block:block_admin_list.html.twig" at line 39."


Solution

  • I have found that this was caused by not having a correctly named service in admin.yml for both bundles.

    When I was getting the error I had sonata.admin.post set for both "Contact" and "Link" entities, but after renaming them as follows my second entry under imports worked a treat.

    Contact admin.yml:

    services:
        sonata.contact.admin.post: # <-- line changed
            class: AyrshireMinis\ContactBundle\Admin\ContactAdmin
            tags:
                - { name: sonata.admin, manager_type: orm, group: "Leads", label: "Contacts" }
            arguments:
                - ~
                - AyrshireMinis\ContactBundle\Entity\Contact
                - ~
            calls:
                - [ setTranslationDomain, [AyrshireMinisContactBundle]]
    

    Link admin.yml:

    services:
        sonata.link.admin.post: # <-- line changed
            class: AyrshireMinis\CommonBundle\Admin\LinkAdmin
            tags:
                - { name: sonata.admin, manager_type: orm, group: "Links", label: "Links" }
            arguments:
                - ~
                - AyrshireMinis\CommonBundle\Entity\Link
                - ~
            calls:
                - [ setTranslationDomain, [AyrshireMinisCommonBundle]]
    

    HTH someone in future. Further reading.