architecture.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. Architecture
  2. ============
  3. The architecture of the ``SonataAdminBundle`` is primarily inspired by the Django Admin
  4. Project, which is truly a great project. More information can be found at the
  5. `Django Project Website`_.
  6. If you followed the instructions on the :doc:`getting_started` page, you should by
  7. now have an ``Admin`` class and an ``Admin`` service. In this chapter, we'll discuss more in
  8. depth how it works.
  9. The Admin Class
  10. ---------------
  11. The ``Admin`` class maps a specific model to the rich CRUD interface provided by
  12. ``SonataAdminBundle``. In other words, using your ``Admin`` classes, you can configure
  13. what is shown by ``SonataAdminBundle`` in each CRUD action for the associated model.
  14. By now you've seen 3 of those actions in the ``getting started`` page: list,
  15. filter and form (for creation/editing). However, a fully configured ``Admin`` class
  16. can define more actions:
  17. ============= =========================================================================
  18. Actions Description
  19. ============= =========================================================================
  20. list The fields displayed in the list table
  21. filter The fields available for filtering the list
  22. form The fields used to create/edit the entity
  23. show The fields used to show the entity
  24. batch actions Actions that can be performed on a group of entities (e.g. bulk delete)
  25. ============= =========================================================================
  26. The ``Sonata\AdminBundle\Admin\Admin`` class is provided as an easy way to
  27. map your models, by extending it. However, any implementation of the
  28. ``Sonata\AdminBundle\Admin\AdminInterface`` can be used to define an ``Admin``
  29. service. For each ``Admin`` service, the following required dependencies are
  30. automatically injected by the bundle:
  31. ========================= =========================================================================
  32. Class Description
  33. ========================= =========================================================================
  34. ConfigurationPool configuration pool where all Admin class instances are stored
  35. ModelManager service which handles specific code relating to your persistence layer (e.g. Doctrine ORM)
  36. FormContractor builds the forms for the edit/create views using the Symfony ``FormBuilder``
  37. ShowBuilder builds the show fields
  38. ListBuilder builds the list fields
  39. DatagridBuilder builds the filter fields
  40. Request the received http request
  41. RouteBuilder allows you to add routes for new actions and remove routes for default actions
  42. RouterGenerator generates the different URLs
  43. SecurityHandler handles permissions for model instances and actions
  44. Validator handles model validation
  45. Translator generates translations
  46. LabelTranslatorStrategy a strategy to use when generating labels
  47. MenuFactory generates the side menu, depending on the current action
  48. ========================= =========================================================================
  49. .. note::
  50. Each of these dependencies is used for a specific task, briefly described above.
  51. If you wish to learn more about how they are used, check the respective documentation
  52. chapter. In most cases, you won't need to worry about their underlying implementation.
  53. All of these dependencies have default values that you can override when declaring any of
  54. your ``Admin`` services. This is done using a ``call`` to the matching ``setter`` :
  55. .. configuration-block::
  56. .. code-block:: xml
  57. <service id="app.admin.post" class="AppBundle\Admin\PostAdmin">
  58. <tag name="sonata.admin" manager_type="orm" group="Content" label="Post" />
  59. <argument />
  60. <argument>AppBundle\Entity\Post</argument>
  61. <argument />
  62. <call method="setLabelTranslatorStrategy">
  63. <argument>sonata.admin.label.strategy.underscore</argument>
  64. </call>
  65. </service>
  66. .. code-block:: yaml
  67. services:
  68. app.admin.post:
  69. class: AppBundle\Admin\PostAdmin
  70. tags:
  71. - { name: sonata.admin, manager_type: orm, group: "Content", label: "Post" }
  72. arguments:
  73. - ~
  74. - AppBundle\Entity\Post
  75. - ~
  76. calls:
  77. - [ setLabelTranslatorStrategy, ["@sonata.admin.label.strategy.underscore"]]
  78. Here, we declare the same ``Admin`` service as in the :doc:`getting_started` chapter, but using a
  79. different label translator strategy, replacing the default one. Notice that
  80. ``sonata.admin.label.strategy.underscore`` is a service provided by ``SonataAdminBundle``,
  81. but you could just as easily use a service of your own.
  82. CRUDController
  83. --------------
  84. The ``CRUDController`` contains the actions you have available to manipulate
  85. your model instances, like create, list, edit or delete. It uses the ``Admin``
  86. class to determine its behavior, like which fields to display in the edit form,
  87. or how to build the list view. Inside the ``CRUDController``, you can access the
  88. ``Admin`` class instance via the ``$admin`` variable.
  89. .. note::
  90. `CRUD is an acronym`_ for "Create, Read, Update and Delete"
  91. The ``CRUDController`` is no different from any other Symfony controller, meaning
  92. that you have all the usual options available to you, like getting services from
  93. the Dependency Injection Container (DIC).
  94. This is particularly useful if you decide to extend the ``CRUDController`` to
  95. add new actions or change the behavior of existing ones. You can specify which controller
  96. to use when declaring the ``Admin`` service by passing it as the 3rd argument. For example
  97. to set the controller to ``AppBundle:PostAdmin``:
  98. .. configuration-block::
  99. .. code-block:: xml
  100. <service id="app.admin.post" class="AppBundle\Admin\PostAdmin">
  101. <tag name="sonata.admin" manager_type="orm" group="Content" label="Post" />
  102. <argument />
  103. <argument>AppBundle\Entity\Post</argument>
  104. <argument>AppBundle:PostAdmin</argument>
  105. <call method="setTranslationDomain">
  106. <argument>AppBundle</argument>
  107. </call>
  108. </service>
  109. .. code-block:: yaml
  110. services:
  111. app.admin.post:
  112. class: AppBundle\Admin\PostAdmin
  113. tags:
  114. - { name: sonata.admin, manager_type: orm, group: "Content", label: "Post" }
  115. arguments:
  116. - ~
  117. - AppBundle\Entity\Post
  118. - AppBundle:PostAdmin
  119. calls:
  120. - [ setTranslationDomain, [AppBundle]]
  121. When extending ``CRUDController``, remember that the ``Admin`` class already has
  122. a set of automatically injected dependencies that are useful when implementing several
  123. scenarios. Refer to the existing ``CRUDController`` actions for examples of how to get
  124. the best out of them.
  125. In your overloaded CRUDController you can overload also these methods to limit
  126. the number of duplicated code from SonataAdmin:
  127. * ``preCreate``: called from ``createAction``
  128. * ``preEdit``: called from ``editAction``
  129. * ``preDelete``: called from ``deleteAction``
  130. * ``preShow``: called from ``showAction``
  131. * ``preList``: called from ``listAction``
  132. These methods are called after checking the access rights and after retrieving the object
  133. from database. You can use them if you need to redirect user to some other page under certain conditions.
  134. Fields Definition
  135. -----------------
  136. Your ``Admin`` class defines which of your model's fields will be available in each
  137. action defined in your ``CRUDController``. So, for each action, a list of field mappings
  138. is generated. These lists are implemented using the ``FieldDescriptionCollection`` class
  139. which stores instances of ``FieldDescriptionInterface``. Picking up on our previous
  140. ``PostAdmin`` class example:
  141. .. code-block:: php
  142. <?php
  143. // src/AppBundle/Admin/PostAdmin.php
  144. namespace AppBundle\Admin;
  145. use Sonata\AdminBundle\Admin\Admin;
  146. use Sonata\AdminBundle\Datagrid\ListMapper;
  147. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  148. use Sonata\AdminBundle\Form\FormMapper;
  149. use Sonata\AdminBundle\Show\ShowMapper;
  150. class PostAdmin extends Admin
  151. {
  152. // Fields to be shown on create/edit forms
  153. protected function configureFormFields(FormMapper $formMapper)
  154. {
  155. $formMapper
  156. ->add('title', 'text', array(
  157. 'label' => 'Post Title'
  158. ))
  159. ->add('author', 'entity', array(
  160. 'class' => 'AppBundle\Entity\User'
  161. ))
  162. // if no type is specified, SonataAdminBundle tries to guess it
  163. ->add('body')
  164. // ...
  165. ;
  166. }
  167. // Fields to be shown on filter forms
  168. protected function configureDatagridFilters(DatagridMapper $datagridMapper)
  169. {
  170. $datagridMapper
  171. ->add('title')
  172. ->add('author')
  173. ;
  174. }
  175. // Fields to be shown on lists
  176. protected function configureListFields(ListMapper $listMapper)
  177. {
  178. $listMapper
  179. ->addIdentifier('title')
  180. ->add('slug')
  181. ->add('author')
  182. ;
  183. }
  184. // Fields to be shown on show action
  185. protected function configureShowFields(ShowMapper $showMapper)
  186. {
  187. $showMapper
  188. ->add('id)
  189. ->add('title')
  190. ->add('slug')
  191. ->add('author')
  192. ;
  193. }
  194. }
  195. Internally, the provided ``Admin`` class will use these three functions to create three
  196. ``FieldDescriptionCollection`` instances:
  197. * ``$formFieldDescriptions``, containing three ``FieldDescriptionInterface`` instances
  198. for title, author and body
  199. * ``$filterFieldDescriptions``, containing two ``FieldDescriptionInterface`` instances
  200. for title and author
  201. * ``$listFieldDescriptions``, containing three ``FieldDescriptionInterface`` instances
  202. for title, slug and author
  203. * ``$showFieldDescriptions``, containing four ``FieldDescriptionInterface`` instances
  204. for id, title, slug and author
  205. The actual ``FieldDescription`` implementation is provided by the storage abstraction
  206. bundle that you choose during the installation process, based on the
  207. ``BaseFieldDescription`` abstract class provided by ``SonataAdminBundle``.
  208. Each ``FieldDescription`` contains various details about a field mapping. Some of
  209. them are independent of the action in which they are used, like ``name`` or ``type``,
  210. while others are used only in specific actions. More information can be found in the
  211. ``BaseFieldDescription`` class file.
  212. In most scenarios, you will not actually need to handle the ``FieldDescription`` yourself.
  213. However, it is important that you know it exists and how it is used, as it sits at the
  214. core of ``SonataAdminBundle``.
  215. Templates
  216. ---------
  217. Like most actions, ``CRUDController`` actions use view files to render their output.
  218. ``SonataAdminBundle`` provides ready to use views as well as ways to easily customize them.
  219. The current implementation uses ``Twig`` as the template engine. All templates
  220. are located in the ``Resources/views`` directory of the bundle.
  221. There are two base templates, one of these is ultimately used in every action:
  222. * ``SonataAdminBundle::standard_layout.html.twig``
  223. * ``SonataAdminBundle::ajax_layout.html.twig``
  224. Like the names say, one if for standard calls, the other one for AJAX.
  225. The subfolders include Twig files for specific sections of ``SonataAdminBundle``:
  226. Block:
  227. ``SonataBlockBundle`` block views. By default there is only one, which
  228. displays all the mapped classes on the dashboard
  229. Button:
  230. Buttons such as ``Add new`` or ``Delete`` that you can see across several
  231. CRUD actions
  232. CRUD:
  233. Base views for every CRUD action, plus several field views for each field type
  234. Core:
  235. Dashboard view, together with deprecated and stub twig files.
  236. Form:
  237. Views related to form rendering
  238. Helper:
  239. A view providing a short object description, as part of a specific form field
  240. type provided by ``SonataAdminBundle``
  241. Pager:
  242. Pagination related view files
  243. These will be discussed in greater detail in the specific :doc:`templates` section, where
  244. you will also find instructions on how to configure ``SonataAdminBundle`` to use your templates
  245. instead of the default ones.
  246. Managing ``Admin`` Service
  247. --------------------------
  248. Your ``Admin`` service definitions are parsed when Symfony is loaded, and handled by
  249. the ``Pool`` class. This class, available as the ``sonata.admin.pool`` service from the
  250. DIC, handles the ``Admin`` classes, lazy-loading them on demand (to reduce overhead)
  251. and matching each of them to a group. It is also responsible for handling the top level
  252. template files, administration panel title and logo.
  253. Create child admins
  254. -------------------
  255. Let us say you have a ``PostAdmin`` and a ``CommentAdmin``. You can optionally declare the ``CommentAdmin``
  256. to be a child of the ``PostAdmin``. This will create new routes like, for example, ``/post/{id}/comment/list``,
  257. where the comments will automatically be filtered by post.
  258. To do this, you first need to call the ``addChild`` method in your PostAdmin service configuration :
  259. .. configuration-block::
  260. .. code-block:: xml
  261. <!-- app/config/config.xml -->
  262. <service id="sonata.news.admin.post" class="Sonata\NewsBundle\Admin\PostAdmin">
  263. <!-- ... -->
  264. <call method="addChild">
  265. <argument type="service" id="sonata.news.admin.comment" />
  266. </call>
  267. </service>
  268. Then, you have to set the CommentAdmin ``parentAssociationMapping`` attribute to ``post`` :
  269. .. code-block:: php
  270. <?php
  271. namespace Sonata\NewsBundle\Admin;
  272. // ...
  273. class CommentAdmin extends Admin
  274. {
  275. protected $parentAssociationMapping = 'post';
  276. // OR
  277. public function getParentAssociationMapping()
  278. {
  279. return 'post';
  280. }
  281. }
  282. It also possible to set a dot-separated value, like ``post.author``, if your parent and child admins are not directly related.
  283. .. _`Django Project Website`: http://www.djangoproject.com/
  284. .. _`CRUD is an acronym`: http://en.wikipedia.org/wiki/CRUD