getting_started.rst 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 persistency 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 any persistency services (service for handling and
  34. controlling your models), however most likely your application will use some
  35. persistency services (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. * SonataPropelAdminBundle
  42. Install a persistency service you need and configure it according to their
  43. related documentation.
  44. Step 3: Create Admin class
  45. --------------------------
  46. Admin class represents mapping of your model and administration sections (forms,
  47. list, show). The easiest way to create an admin class for your model is to extend
  48. the ``Sonata\AdminBundle\Admin\Admin`` class. For filter, list and show views, you can
  49. target a sub model property thanks to the dot-separated notation
  50. (eg: ``mySubModel.mySubSubModel.myProperty``).
  51. Here is a simple example from the SonataNewsBundle:
  52. .. code-block:: php
  53. namespace Sonata\NewsBundle\Admin;
  54. use Sonata\AdminBundle\Admin\Admin;
  55. use Sonata\AdminBundle\Datagrid\ListMapper;
  56. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  57. use Sonata\AdminBundle\Form\FormMapper;
  58. class TagAdmin extends Admin
  59. {
  60. protected function configureFormFields(FormMapper $formMapper)
  61. {
  62. $formMapper
  63. ->add('name')
  64. ->add('enabled', null, array('required' => false))
  65. ;
  66. }
  67. protected function configureDatagridFilters(DatagridMapper $datagridMapper)
  68. {
  69. $datagridMapper
  70. ->add('name')
  71. ->add('posts')
  72. ;
  73. }
  74. protected function configureListFields(ListMapper $listMapper)
  75. {
  76. $listMapper
  77. ->addIdentifier('name')
  78. ->add('slug')
  79. ->add('enabled')
  80. ;
  81. }
  82. }
  83. Step 4: Create admin service
  84. ----------------------------
  85. To notify your administration of your new admin class you need to create an
  86. admin service and link it into the framework by setting the sonata.admin tag.
  87. Create either a new ``admin.xml`` or ``admin.yml`` file inside the ``MyBundle/Resources/config/`` folder:
  88. .. code-block:: xml
  89. <!-- MyBundle/Resources/config/admin.xml -->
  90. <container xmlns="http://symfony.com/schema/dic/services"
  91. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  92. xsi:schemaLocation="http://symfony.com/schema/dic/services/services-1.0.xsd">
  93. <services>
  94. <service id="sonata.admin.tag" class="YourNS\AdminBundle\Admin\BlogAdmin">
  95. <tag name="sonata.admin" manager_type="orm" group="Posts" label="Blog"/>
  96. <argument />
  97. <argument>YourNS\AdminBundle\Entity\Course</argument>
  98. <argument>SonataAdminBundle:CRUD</argument>
  99. <call method="setTranslationDomain">
  100. <argument>YourNSAdminBundle</argument>
  101. </call>
  102. </service>
  103. </services>
  104. </container>
  105. .. code-block:: yaml
  106. # MyBundle/Resources/config/admin.yml
  107. services:
  108. sonata.admin.tag:
  109. class: YourNS\AdminBundle\Admin\BlogAdmin
  110. tags:
  111. - { name: sonata.admin, manager_type: orm, group: posts, label: "Blog" }
  112. arguments:
  113. - ~
  114. - YourNS\AdminBundle\Entity\Course
  115. - 'SonataAdminBundle:CRUD'
  116. calls:
  117. - [ setTranslationDomain, [YourNSAdminBundle]]
  118. Now include your new configuration file in the framework (make sure that your ``resource`` value has the
  119. correct file extension depending on the code block that you used above):
  120. .. code-block:: yaml
  121. # app/config/config.yml
  122. imports:
  123. - { resource: @MyBundle/Resources/config/admin.xml }
  124. Or you can load the file inside with the Bundle's extension file using the ``load()`` method as described
  125. in the `symfony cookbook`_.
  126. .. code-block:: php
  127. # YourNS/AdminBundle/DependencyInjection/YourNSAdminBundleExtension.php for XML configurations
  128. use Symfony\Component\DependencyInjection\Loader;
  129. use Symfony\Component\Config\FileLocator;
  130. public function load(array $configs, ContainerBuilder $container) {
  131. // ...
  132. $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
  133. $loader->load('admin.xml');
  134. }
  135. .. code-block:: php
  136. # YourNS/AdminBundle/DependencyInjection/YourNSAdminBundleExtension.php for YAML configurations
  137. use Symfony\Component\DependencyInjection\Loader;
  138. use Symfony\Component\Config\FileLocator;
  139. public function load(array $configs, ContainerBuilder $container) {
  140. // ...
  141. $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
  142. $loader->load('admin.yml');
  143. }
  144. Step 5: Configuration
  145. ---------------------
  146. At this point you have basic administration for your model. If you wish to
  147. quickly customize your administration you can create some configuration options
  148. and change them according to your requirements:
  149. .. code-block:: yaml
  150. # app/config/config.yml
  151. sonata_admin:
  152. title: Sonata Project
  153. title_logo: /bundles/sonataadmin/logo_title.png
  154. templates:
  155. # default global templates
  156. layout: SonataAdminBundle::standard_layout.html.twig
  157. ajax: SonataAdminBundle::ajax_layout.html.twig
  158. # default actions templates, should extend a global templates
  159. list: SonataAdminBundle:CRUD:list.html.twig
  160. show: SonataAdminBundle:CRUD:show.html.twig
  161. edit: SonataAdminBundle:CRUD:edit.html.twig
  162. dashboard:
  163. blocks:
  164. # display a dashboard block
  165. - { position: left, type: sonata.admin.block.admin_list }
  166. Linking the admin class to the dashboard is done automatically because of the
  167. default option you defined above:
  168. .. code-block:: yaml
  169. dashboard
  170. blocks:
  171. # display a dashboard block
  172. - { position: left, type: sonata.admin.block.admin_list }
  173. However you can define only admin groups you want to show in the dashboard by:
  174. .. code-block:: yaml
  175. dashboard
  176. blocks:
  177. # display a dashboard block
  178. - { position: left, type: sonata.admin.block.admin_list }
  179. groups:
  180. sonata_page:
  181. label: Page
  182. items: ~
  183. More information can be found in the configuration chapter of this documentation.
  184. Step 6: Security
  185. ----------------
  186. The last important step is security. By default, the SonataAdminBundle does not
  187. come with any user management for ultimate flexibility, however it is most
  188. likely your application requires such feature. The Sonata Project includes a
  189. ``SonataUserBundle`` which integrates the very popular ``FOSUserBundle``. Please
  190. refer to the security section of this documentation for more information.
  191. That should be it! Read next sections fore more verbose documentation of the
  192. SonataAdminBundle and how to tweak it for your requirements.
  193. .. _`symfony cookbook`: http://symfony.com/doc/master/cookbook/bundles/extension.html#using-the-load-method