getting_started.rst 9.2 KB

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