Search code examples
phpsymfonyadminsonata

Symfony 3 Sonata Admin create admin using annotation


I try to create a sonata admin using only annotation from JMSDiExtraBundle.

Admin:

/**
 * @DI\Service("sonata.admin.company")
 * @DI\Tag("sonata.admin", attributes = {
 *     "manager_type" = "orm",
 *     "label"="Category",
 *     "group"="Orders"
 *      })
 */
class CompanyAdmin extends Admin
{
    /**
     * @DI\InjectParams({
     *     "code" = @DI\Inject("%admin__company__class%"),
     *     "class" = @DI\Inject("%admin__company__code%"),
     *     "baseControllerName" = @DI\Inject("%admin__company__base_controller_name%")
     * })
     */
    public function __construct($code, $class, $baseControllerName)
    {
        parent::__construct($code, $class, $baseControllerName);
    }
}

And file with parameters:

parameters:
#    parameter_name: value
    admin__company__class: AppBundle\Entity\Company
    admin__company__base_controller_name: SonataAdminBundle:CRUD
    admin__company__code: admin.company

When i register admin in yml it work, but using annotation no.

Tom


Solution

  • As the documentation for JMSDiExtraBundle states:

    By default, you can only use the provided annotations on your non-service controllers; no other directories are scanned.

    However, if you also would like to use annotations to configure your regular services, you can configure more locations as demonstrated below.

    If you would like to configure services in a bundle of yours via annotations, or have some services outside of any bundles structure such as in your src/ directory, you can make use of the following configuration options, so that the bundle will pick them up, and add them to your dependency injection container:

    jms_di_extra:
        locations:
            all_bundles: false
            bundles: [FooBundle, AcmeBlogBundle]
            directories: ["%kernel.root_dir%/../src"]