getting_started.rst 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. Getting started with SonataAdminBundle
  2. ======================================
  3. If you followed the installation instructions, SonataAdminBundle should be installed
  4. but inaccessible. You first need to configure it for your models before you can
  5. start using it. Here is a quick checklist of what is needed to quickly setup
  6. SonataAdminBundle and create your first admin interface for the models of your application:
  7. * Step 1: Define SonataAdminBundle routes
  8. * Step 2: Create an Admin class
  9. * Step 3: Create an Admin service
  10. * Step 4: Configuration
  11. Step 1: Define SonataAdminBundle routes
  12. ---------------------------------------
  13. To be able to access SonataAdminBundle's pages, you need to add its routes
  14. to your application's routing file:
  15. .. configuration-block::
  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. .. note::
  30. For those curious about the ``resource: .`` setting: it is unusual syntax but used
  31. because Symfony requires a resource to be defined (which points to a real file).
  32. Once this validation passes Sonata's ``AdminPoolLoader`` is in charge of processing
  33. this route and it simply ignores the resource setting.
  34. At this point you can already access the (empty) admin dashboard by visiting the url:
  35. ``http://yoursite.local/admin/dashboard``.
  36. Step 2: Create an Admin class
  37. -----------------------------
  38. SonataAdminBundle helps you manage your data using a graphic interface that
  39. will let you create, update or search your model's instances. Those actions need to
  40. be configured, which is done using an Admin class.
  41. An Admin class represents the mapping of your model to each administration action.
  42. In it, you decide which fields to show on a listing, which to use as filters or what
  43. to show on an creation/edition form.
  44. The easiest way to create an Admin class for your model is to extend
  45. the ``Sonata\AdminBundle\Admin\Admin`` class.
  46. Suppose your AcmeDemoBundle has a Post entity. This is how a basic Admin class
  47. for it could look like:
  48. .. code-block:: php
  49. namespace Acme\DemoBundle\Admin;
  50. use Sonata\AdminBundle\Admin\Admin;
  51. use Sonata\AdminBundle\Datagrid\ListMapper;
  52. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  53. use Sonata\AdminBundle\Form\FormMapper;
  54. class PostAdmin extends Admin
  55. {
  56. // Fields to be shown on create/edit forms
  57. protected function configureFormFields(FormMapper $formMapper)
  58. {
  59. $formMapper
  60. ->add('title', 'text', array('label' => 'Post Title'))
  61. ->add('author', 'entity', array('class' => 'Acme\DemoBundle\Entity\User'))
  62. ->add('body') //if no type is specified, SonataAdminBundle tries to guess it
  63. ;
  64. }
  65. // Fields to be shown on filter forms
  66. protected function configureDatagridFilters(DatagridMapper $datagridMapper)
  67. {
  68. $datagridMapper
  69. ->add('title')
  70. ->add('author')
  71. ;
  72. }
  73. // Fields to be shown on lists
  74. protected function configureListFields(ListMapper $listMapper)
  75. {
  76. $listMapper
  77. ->addIdentifier('title')
  78. ->add('slug')
  79. ->add('author')
  80. ;
  81. }
  82. }
  83. Implementing these three functions is the first step to creating an Admin class.
  84. Other options are available, that will let you further customize the way your model
  85. is shown and handled. Those will be covered in more advanced chapters of this manual.
  86. Step 3: Create an Admin service
  87. -------------------------------
  88. Now that you have created your Admin class, you need to create a service for it. This
  89. service needs to have the ``sonata.admin`` tag, which is your way of letting
  90. SonataAdminBundle know that this particular service represents an Admin class:
  91. Create either a new ``admin.xml`` or ``admin.yml`` file inside the ``Acme/DemoBundle/Resources/config/`` folder:
  92. .. configuration-block::
  93. .. code-block:: xml
  94. <!-- Acme/DemoBundle/Resources/config/admin.xml -->
  95. <container xmlns="http://symfony.com/schema/dic/services"
  96. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  97. xsi:schemaLocation="http://symfony.com/schema/dic/services/services-1.0.xsd">
  98. <services>
  99. <service id="sonata.admin.post" class="Acme\DemoBundle\Admin\PostAdmin">
  100. <tag name="sonata.admin" manager_type="orm" group="Content" label="Post"/>
  101. <argument />
  102. <argument>Acme\DemoBundle\Entity\Post</argument>
  103. <argument />
  104. <call method="setTranslationDomain">
  105. <argument>AcmeDemoBundle</argument>
  106. </call>
  107. </service>
  108. </services>
  109. </container>
  110. .. code-block:: yaml
  111. # Acme/DemoBundle/Resources/config/admin.yml
  112. services:
  113. sonata.admin.post:
  114. class: Acme\DemoBundle\Admin\PostAdmin
  115. tags:
  116. - { name: sonata.admin, manager_type: orm, group: "Content", label: "Post" }
  117. arguments:
  118. - ~
  119. - Acme\DemoBundle\Entity\Post
  120. - ~
  121. calls:
  122. - [ setTranslationDomain, [AcmeDemoBundle]]
  123. The basic configuration of an Admin service is quite simple. It creates a service
  124. instance based on the class you specified before, and accepts three arguments:
  125. 1. The Admin service's code (defaults to the service's name)
  126. 2. The model which this Admin class maps (required)
  127. 3. The controller that will handle the administration actions (defaults to SonataAdminBundle:CRUDController)
  128. Usually you just need to specify the second argument, as the first and third's default
  129. values will work for most scenarios.
  130. The ``setTranslationDomain`` call lets you choose which translation domain to use when
  131. translating labels on the admin pages. More info on the `symfony translations page`_.
  132. Now that you have a configuration file with you admin service, you just need to tell
  133. Symfony2 to load it. There are two ways to do so:
  134. 1 - Importing it in the main config.yml
  135. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  136. Include your new configuration file in the main config.yml (make sure that you
  137. use the correct file extension):
  138. .. configuration-block::
  139. .. code-block:: yaml
  140. # app/config/config.yml
  141. imports:
  142. - { resource: @AcmeDemoBundle/Resources/config/admin.xml }
  143. 2 - Have your bundle load it
  144. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  145. You can also have your bundle load the admin configuration file. Inside your bundle's extension
  146. file, using the ``load()`` method as described in the `symfony cookbook`_.
  147. .. configuration-block::
  148. .. code-block:: php
  149. # Acme/DemoBundle/DependencyInjection/AcmeDemoBundleExtension.php for XML configurations
  150. use Symfony\Component\DependencyInjection\Loader;
  151. use Symfony\Component\Config\FileLocator;
  152. public function load(array $configs, ContainerBuilder $container) {
  153. // ...
  154. $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
  155. $loader->load('admin.xml');
  156. }
  157. .. code-block:: php
  158. # Acme/DemoBundle/DependencyInjection/AcmeDemoBundleExtension.php for YAML configurations
  159. use Symfony\Component\DependencyInjection\Loader;
  160. use Symfony\Component\Config\FileLocator;
  161. public function load(array $configs, ContainerBuilder $container) {
  162. // ...
  163. $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
  164. $loader->load('admin.yml');
  165. }
  166. Step 4: Configuration
  167. ---------------------
  168. At this point you have basic administration actions for your model. If you visit ``http://yoursite.local/admin/dashboard`` again, you should now see a panel with
  169. your model mapped. You can start creating, listing, editing and deleting instances.
  170. You probably want to put your own project's name and logo on the top bar.
  171. You can do so on your project's main config.yml file:
  172. .. configuration-block::
  173. .. code-block:: yaml
  174. # app/config/config.yml
  175. sonata_admin:
  176. title: Acme Demo Bundle
  177. title_logo: /bundles/acmedemo/fancy_acme_logo.png
  178. Next steps - Security
  179. ---------------------
  180. As you probably noticed, you were able to access your dashboard and data by just
  181. typing in the URL. By default, the SonataAdminBundle does not come with any user
  182. management for ultimate flexibility. However, it is most likely that your application
  183. requires such feature. The Sonata Project includes a ``SonataUserBundle`` which
  184. integrates the very popular ``FOSUserBundle``. Please refer to the :doc:`security` section of
  185. this documentation for more information.
  186. Congratulations! You are ready to start using SonataAdminBundle. You can now map
  187. additional models or explore advanced functionalities. The following sections will
  188. each address a specific section or functionality of the bundle, giving deeper
  189. details on what can be configured and achieved with SonataAdminBundle.
  190. .. _`symfony cookbook`: http://symfony.com/doc/master/cookbook/bundles/extension.html#using-the-load-method
  191. .. _`symfony translations page`: http://symfony.com/doc/current/book/translation.html#using-message-domains