routing.rst 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. Routing
  2. =======
  3. The default routes used in the CRUD controller are accessible through the
  4. ``Admin`` class.
  5. The ``Admin`` class contains two routing methods:
  6. * ``getRoutes()``: Returns the available routes;
  7. * ``generateUrl($name, $options)``: Generates the related routes.
  8. Routing Definition
  9. ------------------
  10. Route names
  11. ^^^^^^^^^^^
  12. You can set a ``baseRouteName`` property inside your ``Admin`` class. This
  13. represents the route prefix, to which an underscore and the action name will
  14. be added to generate the actual route names. Note: this is the internal *name*
  15. given to a route (it has nothing to do with the route's visible *URL*).
  16. .. code-block:: php
  17. <?php
  18. class PostAdmin extends Admin
  19. {
  20. protected $baseRouteName = 'sonata_post';
  21. // will result in routes named:
  22. // sonata_post_list
  23. // sonata_post_create
  24. // etc..
  25. }
  26. If no ``baseRouteName`` is defined then the Admin will generate one for you,
  27. based on the following format: 'admin_vendor_bundlename_entityname' so you will have
  28. route names for your actions like 'admin_vendor_bundlename_entityname_list'.
  29. If the Admin fails to find a baseRouteName for your Admin class a ``RuntimeException``
  30. will be thrown with a related message.
  31. Route patterns (URLs)
  32. ^^^^^^^^^^^^^^^^^^^^^
  33. You can use ``baseRoutePattern`` to set a custom URL for a given ``Admin`` class.
  34. For example, to use ``http://yourdomain.com/admin/foo`` as the base URL for
  35. the ``FooAdmin`` class (instead of the default of ``http://yourdomain.com/admin/vendor/bundle/foo``)
  36. use the following code:
  37. .. code-block:: php
  38. <?php
  39. class FooAdmin extends Admin
  40. {
  41. protected $baseRouteName = 'foo';
  42. }
  43. You will then have route URLs like ``http://yourdomain.com/admin/foo/list`` and
  44. ``http://yourdomain.com/admin/foo/1/edit``
  45. Routing usage
  46. -------------
  47. Inside a CRUD template, a route for the current ``Admin`` class can be generated via
  48. the admin variable's ``generateUrl()`` command:
  49. .. code-block:: html+jinja
  50. <a href="{{ admin.generateUrl('list') }}">List</a>
  51. <a href="{{ admin.generateUrl('list', params|merge('page': 1) }}">List</a>
  52. Note that you do not need to provide the Admin's route prefix (baseRouteName) to
  53. generate a URL for the current Admin, just the action name.
  54. To generate a URL for a different Admin you just use the Route Name with the usual
  55. Twig helpers:
  56. .. code-block:: html+jinja
  57. <a href="{{ path('admin_acme_demo_foo_list') }}">List</a>
  58. Create a route
  59. --------------
  60. You can register new routes by defining them in your ``Admin`` class. Only Admin
  61. routes should be registered this way.
  62. The routes you define in this way are generated within your Admin's context, and
  63. the only required parameter to ``add()`` is the action name. The second parameter
  64. can be used to define the URL format to append to ``baseRoutePattern``, if not set
  65. explicitly this defaults to the action name.
  66. .. code-block:: php
  67. <?php
  68. use Sonata\AdminBundle\Route\RouteCollection;
  69. class MediaAdmin extends Admin
  70. {
  71. protected function configureRoutes(RouteCollection $collection)
  72. {
  73. $collection->add('myCustomAction');
  74. $collection->add('view', $this->getRouterIdParameter().'/view');
  75. }
  76. }
  77. Other steps needed to create your new action
  78. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  79. In addition to defining the route for your new action you also need to create a
  80. handler for it in your Controller. By default Admin classes use ``SonataAdminBundle:CRUD``
  81. as their controller, but this can be changed by altering the third argument when defining
  82. your Admin service (in your admin.yml file).
  83. For example, lets change the Controller for our MediaAdmin class to AcmeDemoBundle:MediaCRUD:
  84. .. code-block:: jinja
  85. # src/Acme/DemoBundle/Resources/config/admin.yml
  86. sonata.admin.media:
  87. class: Acme\DemoBundle\Admin\MediaAdmin
  88. tags:
  89. - { name: sonata.admin, manager_type: orm, label: "Media" }
  90. arguments:
  91. - ~
  92. - Acme\DemoBundle\Entity\Page
  93. - 'AcmeDemoBundle:MediaCRUD' # define the new controller via the third argument
  94. calls:
  95. - [ setTranslationDomain, [Acme\DemoBundle]]
  96. We now need to create our Controller, the easiest way is to extend the basic Sonata CRUD controller:
  97. .. code-block:: php
  98. <?php
  99. // /src/Acme/DemoBundle/Controller/MediaCRUDController.php
  100. namespace Acme\DemoBundle\Controller;
  101. use Sonata\AdminBundle\Controller\CRUDController;
  102. class MediaCRUDController extends CRUDController
  103. {
  104. public function myCustomAction()
  105. {
  106. // your code here ...
  107. }
  108. }
  109. Removing a route
  110. --------------
  111. Extending ``Sonata\AdminBundle\Admin\Admin`` will give your Admin classes the following
  112. default routes:
  113. * batch
  114. * create
  115. * delete
  116. * export
  117. * edit
  118. * list
  119. * show
  120. You can view all of the current routes defined for an Admin class by using the console to run
  121. ``php app/console sonata:admin:explain <<admin.service.name>>``
  122. for example if your Admin is called sonata.admin.foo you would run
  123. ``php app/console sonata:admin:explain sonata.admin.foo``
  124. Sonata internally checks for the existence of a route before linking to it. As a result, removing a
  125. route will prevent links to that action from appearing in the administrative interface. For example,
  126. removing the 'create' route will prevent any links to "Add new" from appearing.
  127. Removing a single route
  128. ^^^^^^^^^^^^^^^^^^^^^^^
  129. Any single registered route can be easily removed by name:
  130. .. code-block:: php
  131. <?php
  132. use Sonata\AdminBundle\Route\RouteCollection;
  133. class MediaAdmin extends Admin
  134. {
  135. protected function configureRoutes(RouteCollection $collection)
  136. {
  137. $collection->remove('delete');
  138. }
  139. }
  140. Removing all routes except named ones
  141. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  142. If you want to disable all default Sonata routes except few whitelisted ones, you can use
  143. the ``clearExcept()`` method. This method accepts an array of routes you want to keep active.
  144. .. code-block:: php
  145. <?php
  146. use Sonata\AdminBundle\Route\RouteCollection;
  147. class MediaAdmin extends Admin
  148. {
  149. protected function configureRoutes(RouteCollection $collection)
  150. {
  151. //Only `list` and `edit` route will be active
  152. $collection->clearExcept(array('list', 'edit'));
  153. }
  154. }
  155. Removing all routes
  156. ^^^^^^^^^^^^^^^^^^^
  157. If you want to remove all default routes, you can use ``clear()`` method.
  158. .. code-block:: php
  159. <?php
  160. use Sonata\AdminBundle\Route\RouteCollection;
  161. class MediaAdmin extends Admin
  162. {
  163. protected function configureRoutes(RouteCollection $collection)
  164. {
  165. //All routes are removed
  166. $collection->clear();
  167. }
  168. }
  169. Removing routes only when an Admin is embedded
  170. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  171. To prevent some routes from being available when one Admin is embedded inside another one
  172. (e.g. to remove the "add new" option when you embed ``TagAdmin`` within ``PostAdmin``) you
  173. can use ``hasParentFieldDescription()`` to detect this case and remove the routes.
  174. .. code-block:: php
  175. <?php
  176. use Sonata\AdminBundle\Route\RouteCollection;
  177. class TagAdmin extends Admin
  178. {
  179. protected function configureRoutes(RouteCollection $collection)
  180. {
  181. if($this->hasParentFieldDescription()) { // prevent display of "Add new" when embedding this form
  182. $collection->remove('create');
  183. }
  184. }
  185. }
  186. Persistent parameters
  187. ---------------------
  188. In some cases, the interface might be required to pass the same parameters
  189. across the different ``Admin``'s actions. Instead of setting them in the
  190. template or doing other weird hacks, you can define a ``getPersistentParameters``
  191. method. This method will be used when a link is being generated.
  192. .. code-block:: php
  193. <?php
  194. class MediaAdmin extends Admin
  195. {
  196. public function getPersistentParameters()
  197. {
  198. if (!$this->getRequest()) {
  199. return array();
  200. }
  201. return array(
  202. 'provider' => $this->getRequest()->get('provider'),
  203. 'context' => $this->getRequest()->get('context', 'default'),
  204. );
  205. }
  206. }
  207. // the result:
  208. // $admin->generateUrl('create') => /admin/module/create?context=default
  209. Changing the default route in a List Action
  210. -------------------------------------------
  211. Usually the identifier column of a list action links to the edit screen. To change the
  212. list action's links to point to a different action, set the ``route`` option in your call to
  213. ``ListMapper::addIdentifier()``. For example, to link to show instead of edit:
  214. .. code-block:: php
  215. <?php
  216. class PostAdmin extends Admin
  217. {
  218. public function configureListFields(ListMapper $listMapper)
  219. {
  220. $listMapper
  221. ->addIdentifier('name', null, array(
  222. 'route' => array('name' => 'show')
  223. ));
  224. }
  225. }