getting_started.rst 8.9 KB

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