Fix for: There is no `_sonata_admin` defined for the controller …

Posted December 25th, 2012 in Razno by Metod

If you stumbled upon the following error while trying to make SonataAdminBundle work:

There is no `_sonata_admin` defined for the controller `Acme\DemoBundle\Controller\PostController` and the current route `admin_acme_demo_post_list`

Here is what is wrong with your configuration.

The official documentation (in my case for ORM) says to define the *Admin service as follows:

# app/config/config.yml
services:
   acme.demo.admin.post:
      class: Acme\DemoBundle\Admin\PostAdmin
      tags:
        - { name: sonata.admin, manager_type: orm, group: acme_demo, label: post }
      arguments: [null, Acme\DemoBundle\Entity\Post, AcmeDemoBundle:PostAdmin]

Notice that null in the arguments? That is what is wrong.

That means that as the code for the admin it passes null. Whereas the correct thing to do would be to pass in the name of the service itself. Like this:

# app/config/config.yml
services:
   acme.demo.admin.post:
      class: Acme\DemoBundle\Admin\PostAdmin
      tags:
        - { name: sonata.admin, manager_type: orm, group: acme_demo, label: post }
      arguments: [acme.demo.admin.post, Acme\DemoBundle\Entity\Post, AcmeDemoBundle:PostAdmin]

Happy coding!