architecture.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. * ``list``: The fields displayed in the list table
  18. * ``filter``: The fields available for filtering the list
  19. * ``form``: The fields used to create/edit the entity
  20. * ``show``: The fields used to show the entity
  21. * Batch actions: Actions that can be performed on a group of entities
  22. (e.g. bulk delete)
  23. The ``Sonata\AdminBundle\Admin\Admin`` class is provided as an easy way to
  24. map your models, by extending it. However, any implementation of the
  25. ``Sonata\AdminBundle\Admin\AdminInterface`` can be used to define an ``Admin``
  26. service. For each ``Admin`` service, the following required dependencies are
  27. automatically injected by the bundle:
  28. * ``ConfigurationPool``: configuration pool where all Admin class instances are stored
  29. * ``ModelManager``: service which handles specific code relating to your persistence layer (e.g. Doctrine ORM)
  30. * ``FormContractor``: builds the forms for the edit/create views using the Symfony ``FormBuilder``
  31. * ``ShowBuilder``: builds the show fields
  32. * ``ListBuilder``: builds the list fields
  33. * ``DatagridBuilder``: builds the filter fields
  34. * ``Request``: the http request received
  35. * ``RouteBuilder``: allows you to add routes for new actions and remove routes for default actions
  36. * ``RouterGenerator``: generates the different urls
  37. * ``SecurityHandler``: handles permissions for model instances and actions
  38. * ``Validator``: handles model validation
  39. * ``Translator``: generates translations
  40. * ``LabelTranslatorStrategy``: a strategy to use when generating labels
  41. * ``MenuFactory``: generates the side menu, depending on the current action
  42. .. note::
  43. Each of these dependencies is used for a specific task, briefly described above.
  44. If you wish to learn more about how they are used, check the respective documentation
  45. chapter. In most cases, you won't need to worry about their underlying implementation.
  46. All of these dependencies have default values that you can override when declaring any of
  47. your ``Admin`` services. This is done using a ``call`` to the matching "setter":
  48. .. code-block:: xml
  49. <service id="sonata.admin.post" class="Acme\DemoBundle\Admin\PostAdmin">
  50. <tag name="sonata.admin" manager_type="orm" group="Content" label="Post"/>
  51. <argument />
  52. <argument>Acme\DemoBundle\Entity\Post</argument>
  53. <argument />
  54. <call method="setLabelTranslatorStrategy">
  55. <argument>sonata.admin.label.strategy.underscore</argument>
  56. </call>
  57. </service>
  58. .. code-block:: yaml
  59. services:
  60. sonata.admin.post:
  61. class: Acme\DemoBundle\Admin\PostAdmin
  62. tags:
  63. - { name: sonata.admin, manager_type: orm, group: "Content", label: "Post" }
  64. arguments:
  65. - ~
  66. - Acme\DemoBundle\Entity\Post
  67. - ~
  68. calls:
  69. - [ setLabelTranslatorStrategy, [sonata.admin.label.strategy.underscore]]
  70. Here, we declare the same ``Admin`` service as in the :doc:`getting_started` chapter, but using a
  71. different label translator strategy, replacing the default one. Notice that
  72. ``sonata.admin.label.strategy.underscore`` is a service provided by ``SonataAdminBundle``,
  73. but you could just as easily use a service of your own.
  74. CRUDController
  75. --------------
  76. The ``CRUDController`` contains the actions you have available to manipulate
  77. your model instances, like create, list, edit or delete. It uses the ``Admin``
  78. class to determine its behavior, like which fields to display in the edit form,
  79. or how to build the list view. Inside the ``CRUDController``, you can access the
  80. ``Admin`` class instance via the ``$admin`` variable.
  81. .. note::
  82. `CRUD is an acronym`_ for "Create, Read, Update and Delete"
  83. The ``CRUDController`` is no different to any other Symfony2 controller, meaning
  84. that you have all the usual options available to you, like getting services from
  85. the Dependency Injection Container (DIC).
  86. This is particulary useful if you decide to extend the ``CRUDController`` to
  87. add new actions or change the behavior of existing ones. You can specify which controller
  88. to use when declaring the ``Admin`` service by passing it as the 3rd argument. For example
  89. to set the controller to ``AcmeDemoBundle:PostAdmin``:
  90. .. code-block:: xml
  91. <services>
  92. <service id="sonata.admin.post" class="Acme\DemoBundle\Admin\PostAdmin">
  93. <tag name="sonata.admin" manager_type="orm" group="Content" label="Post"/>
  94. <argument />
  95. <argument>Acme\DemoBundle\Entity\Post</argument>
  96. <argument>AcmeDemoBundle:PostAdmin</argument>
  97. <call method="setTranslationDomain">
  98. <argument>AcmeDemoBundle</argument>
  99. </call>
  100. </service>
  101. </services>
  102. .. code-block:: yaml
  103. services:
  104. sonata.admin.post:
  105. class: Acme\DemoBundle\Admin\PostAdmin
  106. tags:
  107. - { name: sonata.admin, manager_type: orm, group: "Content", label: "Post" }
  108. arguments:
  109. - ~
  110. - Acme\DemoBundle\Entity\Post
  111. - AcmeDemoBundle:PostAdmin
  112. calls:
  113. - [ setTranslationDomain, [AcmeDemoBundle]]
  114. When extending ``CRUDController``, remember that the ``Admin`` class already has
  115. a set of automatically injected dependencies that are useful when implementing several
  116. scenarios. Refer to the existing ``CRUDController`` actions for examples of how to get
  117. the best out of them.
  118. Fields Definition
  119. -----------------
  120. Your ``Admin`` class defines which of your model's fields will be available in each
  121. action defined in your ``CRUDController``. So, for each action, a list of field mappings
  122. is generated. These lists are implemented using the ``FieldDescriptionCollection`` class
  123. which stores instances of ``FieldDescriptionInterface``. Picking up on our previous
  124. ``PostAdmin`` class example:
  125. .. code-block:: php
  126. namespace Acme\DemoBundle\Admin;
  127. use Sonata\AdminBundle\Admin\Admin;
  128. use Sonata\AdminBundle\Datagrid\ListMapper;
  129. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  130. use Sonata\AdminBundle\Form\FormMapper;
  131. class PostAdmin extends Admin
  132. {
  133. //Fields to be shown on create/edit forms
  134. protected function configureFormFields(FormMapper $formMapper)
  135. {
  136. $formMapper
  137. ->add('title', 'text', array('label' => 'Post Title'))
  138. ->add('author', 'entity', array('class' => 'Acme\DemoBundle\Entity\User'))
  139. ->add('body') //if no type is specified, SonataAdminBundle tries to guess it
  140. ;
  141. }
  142. //Fields to be shown on filter forms
  143. protected function configureDatagridFilters(DatagridMapper $datagridMapper)
  144. {
  145. $datagridMapper
  146. ->add('title')
  147. ->add('author')
  148. ;
  149. }
  150. //Fields to be shown on lists
  151. protected function configureListFields(ListMapper $listMapper)
  152. {
  153. $listMapper
  154. ->addIdentifier('title')
  155. ->add('slug')
  156. ->add('author')
  157. ;
  158. }
  159. }
  160. Internally, the provided ``Admin`` class will use these three functions to create three
  161. ``FieldDescriptionCollection`` instances:
  162. * ``$formFieldDescriptions``, containing three ``FieldDescriptionInterface`` instances
  163. for title, author and body
  164. * ``$filterFieldDescriptions``, containing two ``FieldDescriptionInterface`` instances
  165. for title and author
  166. * ``$listFieldDescriptions``, containing three ``FieldDescriptionInterface`` instances
  167. for title, slug and author
  168. The actual ``FieldDescription`` implementation is provided by the storage abstraction
  169. bundle that you choose during the installation process, based on the
  170. ``BaseFieldDescription`` abstract class provided by ``SonataAdminBundle``.
  171. Each ``FieldDescription`` contains various details about a field mapping. Some of
  172. them are independent of the action in which they are used, like ``name`` or ``type``,
  173. while other are used only in specific actions. More information can be found in the
  174. ``BaseFieldDescription`` class file.
  175. In most scenarios, you won't actually need to handle the ``FieldDescription`` yourself.
  176. However, it is important that you know it exists and how it's used, as it sits at the
  177. core of ``SonataAdminBundle``.
  178. Templates
  179. ---------
  180. Like most actions, ``CRUDController`` actions use view files to render their output.
  181. ``SonataAdminBundle`` provides ready to use views as well as ways to easily customize them.
  182. The current implementation uses ``Twig`` as the template engine. All templates
  183. are located in the ``Resources/views`` directory of the bundle.
  184. There are two base templates, one of these is ultimately used in every action:
  185. * ``SonataAdminBundle::standard_layout.html.twig``
  186. * ``SonataAdminBundle::ajax_layout.html.twig``
  187. Like the names say, one if for standard calls, the other one for AJAX.
  188. The subfolders include Twig files for specific sections of ``SonataAdminBundle``:
  189. Block:
  190. ``SonataBlockBundle`` block views. By default there is only has one, which
  191. displays all the mapped classes on the dashboard
  192. Button:
  193. Buttons such as ``Add new`` or ``Delete`` that you can see across several
  194. CRUD actions
  195. CRUD:
  196. Base views for every CRUD action, plus several field views for each field type
  197. Core:
  198. Dashboard view, together with deprecated and stub twig files.
  199. Form:
  200. Views related to form rendering
  201. Helper:
  202. A view providing a short object description, as part of a specific form field
  203. type provided by ``SonataAdminBundle``
  204. Pager:
  205. Pagination related view files
  206. These will be discussed in greater detail in the specific :doc:`templates` section, where
  207. you will also find instructions on how to configure ``SonataAdminBundle`` to use your templates
  208. instead of the default ones.
  209. Managing ``Admin`` Service
  210. ------------------------------
  211. Your ``Admin`` service definitions are parsed when Symfony2 is loaded, and handled by
  212. the ``Pool`` class. This class, available as the ``sonata.admin.pool`` service from the
  213. DIC, handles the ``Admin`` classes, lazy-loading them on demand (to reduce overhead)
  214. and matching each of them to a group. It's also responsible for handling the top level
  215. template files, administration panel title and logo.
  216. .. _`Django Project Website`: http://www.djangoproject.com/
  217. .. _`CRUD is an acronym`: http://en.wikipedia.org/wiki/CRUD