getting_started.rst 11 KB

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