getting_started.rst 9.8 KB

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