installation.rst 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. Installation
  2. ============
  3. SonataAdminBundle is just a bundle and as such, you can install it at any
  4. moment during a project's lifecycle.
  5. 1. Download the Bundle
  6. ----------------------
  7. Open a command console, enter your project directory and execute the
  8. following command to download the latest stable version of this bundle:
  9. .. code-block:: bash
  10. $ composer require sonata-project/admin-bundle
  11. This command requires you to have Composer installed globally, as explained in
  12. the `installation chapter`_ of the Composer documentation.
  13. 1.1. Download a Storage Bundle
  14. ------------------------------
  15. You've now downloaded the SonataAdminBundle. While this bundle contains all
  16. functionality, it needs storage bundles to be able to communicate with a
  17. database. Before using the SonataAdminBundle, you have to download one of these
  18. storage bundles. The official storage bundles are:
  19. * `SonataDoctrineORMAdminBundle`_ (integrates the Doctrine ORM);
  20. * `SonataDoctrineMongoDBAdminBundle`_ (integrates the Doctrine MongoDB ODM);
  21. * `SonataPropelAdminBundle`_ (integrates Propel);
  22. * `SonataDoctrinePhpcrAdminBundle`_ (integrates the Doctrine PHPCR ODM).
  23. You can download them in the same way as the SonataAdminBundle. For instance,
  24. to download the SonataDoctrineORMAdminBundle, execute the following command:
  25. .. code-block:: bash
  26. $ composer require sonata-project/doctrine-orm-admin-bundle
  27. .. tip::
  28. Don't know which to choose? Most new users prefer SonataDoctrineORMAdmin,
  29. to interact with traditional relational databases (MySQL, PostgreSQL, etc).
  30. Step 2: Enable the Bundle
  31. -------------------------
  32. Then, enable the bundle and the bundles it relies on by adding the following
  33. line in the `app/AppKernel.php` file of your project:
  34. .. code-block:: php
  35. // app/AppKernel.php
  36. // ...
  37. class AppKernel extends Kernel
  38. {
  39. public function registerBundles()
  40. {
  41. $bundles = array(
  42. // ...
  43. // The admin requires some twig functions defined in the security
  44. // bundle, like is_granted. Register this bundle if it wasn't the case
  45. // already.
  46. new Symfony\Bundle\SecurityBundle\SecurityBundle(),
  47. // These are the other bundles the SonataAdminBundle relies on
  48. new Sonata\CoreBundle\SonataCoreBundle(),
  49. new Sonata\BlockBundle\SonataBlockBundle(),
  50. new Knp\Bundle\MenuBundle\KnpMenuBundle(),
  51. // And finally, the storage and SonataAdminBundle
  52. new Sonata\AdminBundle\SonataAdminBundle(),
  53. );
  54. // ...
  55. }
  56. // ...
  57. }
  58. .. note::
  59. If a bundle is already registered somewhere in your ``AppKernel.php``, you
  60. should not register it again.
  61. .. note::
  62. Since version 2.3, the bundle comes with jQuery and other front-end
  63. libraries. To update the versions (which isn't required), you can use
  64. `Bower`_. To make sure you get the dependencies that match the version of
  65. SonataAdminBundle you are using, you can make bower use the local bower
  66. dependency file, like this:
  67. .. code-block:: bash
  68. $ bower install ./vendor/sonata-project/admin-bundle/bower.json
  69. .. note::
  70. You must enable translator service in `config.yml`.
  71. .. code-block:: yaml
  72. framework:
  73. translator: { fallbacks: ["%locale%"] }
  74. For more information: http://symfony.com/doc/current/translation.html#configuration
  75. Step 3: Configure the Installed Bundles
  76. ---------------------------------------
  77. Now all needed bundles are downloaded and registered, you have to add some
  78. configuration. The admin interface is using SonataBlockBundle to put everything
  79. in blocks. You just have to tell the block bundle about the existence of the
  80. admin block:
  81. .. code-block:: yaml
  82. # app/config/config.yml
  83. sonata_block:
  84. default_contexts: [cms]
  85. blocks:
  86. # enable the SonataAdminBundle block
  87. sonata.admin.block.admin_list:
  88. contexts: [admin]
  89. # ...
  90. .. note::
  91. Don't worry too much if, at this point, you don't yet understand fully
  92. what a block is. The SonataBlockBundle is a useful tool, but it's not vital
  93. that you understand it in order to use the admin bundle.
  94. Step 4: Import Routing Configuration
  95. ------------------------------------
  96. The bundles are now registered and configured correctly. Before you can use it,
  97. the Symfony router needs to know the routes provided by the SonataAdminBundle.
  98. You can do this by importing them in the routing configuration:
  99. .. code-block:: yaml
  100. # app/config/routing.yml
  101. admin_area:
  102. resource: "@SonataAdminBundle/Resources/config/routing/sonata_admin.xml"
  103. prefix: /admin
  104. Step 5: Enable the "translator" service
  105. ---------------------------------------
  106. The translator service is required by SonataAdmin to display all labels properly.
  107. .. code-block:: yaml
  108. # app/config/config.yml
  109. framework:
  110. translator: { fallbacks: [en] }
  111. Step 6: Define routes
  112. ---------------------
  113. To be able to access SonataAdminBundle's pages, you need to add its routes
  114. to your application's routing file:
  115. .. configuration-block::
  116. .. code-block:: yaml
  117. # app/config/routing.yml
  118. admin:
  119. resource: '@SonataAdminBundle/Resources/config/routing/sonata_admin.xml'
  120. prefix: /admin
  121. _sonata_admin:
  122. resource: .
  123. type: sonata_admin
  124. prefix: /admin
  125. .. note::
  126. If you're using XML or PHP to specify your application's configuration,
  127. the above routing configuration must be placed in routing.xml or
  128. routing.php according to your format (i.e. XML or PHP).
  129. .. note::
  130. For those curious about the ``resource: .`` setting: it is unusual syntax but used
  131. because Symfony requires a resource to be defined (which points to a real file).
  132. Once this validation passes Sonata's ``AdminPoolLoader`` is in charge of processing
  133. this route and it simply ignores the resource setting.
  134. At this point you can already access the (empty) admin dashboard by visiting the URL:
  135. ``http://yoursite.local/admin/dashboard``.
  136. Step 7: Preparing your Environment
  137. ----------------------------------
  138. As with all bundles you install, it's a good practice to clear the cache and
  139. install the assets:
  140. .. code-block:: bash
  141. $ php bin/console cache:clear
  142. $ php bin/console assets:install
  143. The Admin Interface
  144. -------------------
  145. You've finished the installation process, congratulations. If you fire up the
  146. server, you can now visit the admin page on http://localhost:8000/admin
  147. .. note::
  148. This tutorial assumes you are using the build-in server using the
  149. ``php bin/console server:start`` (or ``server:run``) command.
  150. .. image:: ../images/getting_started_empty_dashboard.png
  151. As you can see, the admin panel is very empty. This is because no bundle has
  152. provided admin functionality for the admin bundle yet. Fortunately, you'll
  153. learn how to do this in the :doc:`next chapter <creating_an_admin>`.
  154. .. _`installation chapter`: https://getcomposer.org/doc/00-intro.md
  155. .. _SonataDoctrineORMAdminBundle: http://sonata-project.org/bundles/doctrine-orm-admin/master/doc/index.html
  156. .. _SonataDoctrineMongoDBAdminBundle: http://sonata-project.org/bundles/mongo-admin/master/doc/index.html
  157. .. _SonataPropelAdminBundle: http://sonata-project.org/bundles/propel-admin/master/doc/index.html
  158. .. _SonataDoctrinePhpcrAdminBundle: http://sonata-project.org/bundles/doctrine-phpcr-admin/master/doc/index.html
  159. .. _Bower: http://bower.io/