getting_started.rst 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. Getting started with SonataAdminBundle
  2. ======================================
  3. After installation of SonataAdminBundle you need to configure it for your models.
  4. Here is a quick checklist of what is needed to quickly setup SonataAdminBundle
  5. and create your first admin interface for the models of your application:
  6. * Step 1: Define SonataAdminBundle routes
  7. * Step 2: Setup the persistence service (ORM, ODM, ...)
  8. * Step 3: Create admin class
  9. * Step 4: Create admin service
  10. * Step 5: Configuration
  11. * Step 6: Security
  12. Step 1: Define SonataAdminBundle routes
  13. ---------------------------------------
  14. SonataAdminBundle contains several routes. Import them by adding the following
  15. code to your application's routing file:
  16. .. code-block:: yaml
  17. # app/config/routing.yml
  18. admin:
  19. resource: '@SonataAdminBundle/Resources/config/routing/sonata_admin.xml'
  20. prefix: /admin
  21. _sonata_admin:
  22. resource: .
  23. type: sonata_admin
  24. prefix: /admin
  25. .. note::
  26. If you're using XML or PHP to specify your application's configuration,
  27. the above routing configuration must be placed in routing.xml or
  28. routing.php according to your format (i.e. XML or PHP).
  29. At this point you can already access the admin dashboard by visiting the url:
  30. ``http://yoursite.local/admin/dashboard``.
  31. Step 2: Setup the persistence service (ORM, ODM, ...)
  32. -----------------------------------------------------
  33. SonataAdminBundle does not impose persistance service (service for handling and
  34. controlling your models), however most likely your application will use some
  35. persistance service (like ORM or ODM for database and document stores) therefore
  36. you can use the following bundles officially supported by Sonata Project's admin
  37. bundle:
  38. * SonataDoctrineORMAdminBundle
  39. * SonataDoctrineMongoDBAdminBundle
  40. * SonataDoctrinePhpcrAdminBundle
  41. Propel users are warmly welcome to contribute and create a new bundle for Propel
  42. ORM that will be integrated in SonataAdminBundle.
  43. Install a persistance servise you need and configure it according to their
  44. related documentation.
  45. Step 3: Create Admin class
  46. --------------------------
  47. Admin class represents mapping of your model and administration sections (forms,
  48. list, show). The easiest way to create an admin class for your model is to extend
  49. the Sonata\AdminBundle\Admin\Admin class. For filter, list and show views, you can
  50. target a sub model property thanks to the dot-separated notation
  51. (eg: ``mySubModel.mySubSubModel.myProperty``).
  52. Here is a simple example from the SonataNewsBundle:
  53. .. code-block:: php
  54. namespace Sonata\NewsBundle\Admin;
  55. use Sonata\AdminBundle\Admin\Admin;
  56. use Sonata\AdminBundle\Datagrid\ListMapper;
  57. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  58. use Sonata\AdminBundle\Validator\ErrorElement;
  59. use Sonata\AdminBundle\Form\FormMapper;
  60. class TagAdmin extends Admin
  61. {
  62. protected function configureFormFields(FormMapper $formMapper)
  63. {
  64. $formMapper
  65. ->add('name')
  66. ->add('enabled', null, array('required' => false))
  67. ;
  68. }
  69. protected function configureDatagridFilters(DatagridMapper $datagridMapper)
  70. {
  71. $datagridMapper
  72. ->add('name')
  73. ->add('posts')
  74. ;
  75. }
  76. protected function configureListFields(ListMapper $listMapper)
  77. {
  78. $listMapper
  79. ->addIdentifier('name')
  80. ->add('slug')
  81. ->add('enabled')
  82. ;
  83. }
  84. public function validate(ErrorElement $errorElement, $object)
  85. {
  86. $errorElement
  87. ->with('name')
  88. ->assertMaxLength(array('limit' => 32))
  89. ->end()
  90. ;
  91. }
  92. }
  93. Step 4: Create admin service
  94. ----------------------------
  95. To notify your administration of your new admin class you need to create an
  96. admin service and link it into the framework by setting the sonata.admin tag.
  97. .. code-block:: xml
  98. <container xmlns="http://symfony.com/schema/dic/services"
  99. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  100. xsi:schemaLocation="http://symfony.com/schema/dic/services/services-1.0.xsd">
  101. <services>
  102. <service id="sonata.admin.course" class="YourNS\AdminBundle\Admin\BlogAdmin">
  103. <tag name="sonata.admin" manager_type="orm" group="Posts" label="Blog"/>
  104. <argument />
  105. <argument>YourNS\AdminBundle\Entity\Course</argument>
  106. <argument>SonataAdminBundle:CRUD</argument>
  107. <call method="setTranslationDomain">
  108. <argument>YourNSAdminBundle</argument>
  109. </call>
  110. </service>
  111. </services>
  112. </container>
  113. Note: If you don't already have a configuration file for the purpose, you can register this service in a ``services.xml`` file, save the file in ``app/config``, and then import it from ``config.yml``:
  114. .. code-block:: yaml
  115. # app/config/config.yml
  116. imports:
  117. - { resource: services.xml }
  118. Step 5: Configuration
  119. ---------------------
  120. At this point you have basic administration for your model. If you wish to
  121. quickly customize your administration you can create some configuration options
  122. and change them according to your requirements:
  123. .. code-block:: yaml
  124. # app/config/config.yml
  125. sonata_admin:
  126. title: Sonata Project
  127. title_logo: /bundles/sonataadmin/logo_title.png
  128. templates:
  129. # default global templates
  130. layout: SonataAdminBundle::standard_layout.html.twig
  131. ajax: SonataAdminBundle::ajax_layout.html.twig
  132. # default actions templates, should extend a global templates
  133. list: SonataAdminBundle:CRUD:list.html.twig
  134. show: SonataAdminBundle:CRUD:show.html.twig
  135. edit: SonataAdminBundle:CRUD:edit.html.twig
  136. dashboard:
  137. blocks:
  138. # display a dashboard block
  139. - { position: left, type: sonata.admin.block.admin_list }
  140. groups:
  141. default: ~
  142. Linking the admin class to the dashboard is done automatically because of the
  143. default option you defined above:
  144. dashboard
  145. blocks:
  146. # display a dashboard block
  147. - { position: left, type: sonata.admin.block.admin_list }
  148. groups:
  149. default: ~
  150. However you can define only admin groups you want to show in the dashboard by:
  151. dashboard
  152. blocks:
  153. # display a dashboard block
  154. - { position: left, type: sonata.admin.block.admin_list }
  155. groups:
  156. sonata_page:
  157. label: Page
  158. items: ~
  159. More information can be found in the configuration chapter of this documentation.
  160. Step 6: Security
  161. ----------------
  162. The last important step is security. By default, the SonataAdminBundle does not
  163. come with any user management for ultimate flexibility, however it is most
  164. likely your application requires such feature. The Sonata Project includes a
  165. ``SonataUserBundle`` which integrates the very popular ``FOSUserBundle``. Please
  166. refer to the security section of this documentation for more information.
  167. That should be it! Read next sections fore more verbose documentation of the
  168. SonataAdminBundle and how to tweak it for your requirements.