Tag Archive: Controller

Inject a custom Controller in Sonata

I had this problem, I needed to inject a Storage to my controller so I can create a custom action and connect to my db using a storage service.

All I needed was configure the services as following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
services:
  app.admin.document
:
    class
: AppBundle\Admin\DocumentAdmin
    arguments
: [~, AppBundle\Entity\Document, 'app.controller.document']
    tags
:
      - { name
: sonata.admin, manager_type: orm, group: Documentation, label: Documents }

app.controller.document
:
    class
: AppBundle\Controller\DocumentAdminController
    calls
:
      - [ setContainer,[ "@service_container" ] ]
    arguments
:
      - "@security.token_storage"
      - "@app.storage.document"

app.repository.document
:
    class
: AppBundle\Repository\DocumentRepository
    factory
: ["@doctrine.orm.entity_manager", getRepository]
    arguments
:
      - AppBundle\Entity\Document

app.storage.document
:
    class
: AppBundle\Storage\DocumentStorage
    arguments
:
      - "@app.repository.document"