routing.rst 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 can be generated by using the ``Admin`` class.
  48. .. code-block:: html+jinja
  49. <a href="{{ admin.generateUrl('list') }}">List</a>
  50. <a href="{{ admin.generateUrl('list', params|merge('page': 1) }}">List</a>
  51. Note that you do not need to provide the Admin's route prefix (baseRouteName) to
  52. generate the URL, just the action name.
  53. Create a route
  54. --------------
  55. You can register new routes by defining them in your ``Admin`` class. Only Admin
  56. routes should be registered this way. Of course this requires the related action
  57. to be defined in the controller.
  58. The routes you define in this way are generated within your Admin's context, and
  59. the only required parameter to ``add()`` is the action name. The second parameter
  60. can be used to define the URL format to append to ``baseRoutePattern``, if not set
  61. explicitly this defaults to the action name.
  62. .. code-block:: php
  63. <?php
  64. use Sonata\AdminBundle\Route\RouteCollection;
  65. class MediaAdmin extends Admin
  66. {
  67. protected function configureRoutes(RouteCollection $collection)
  68. {
  69. $collection->add('duplicate');
  70. $collection->add('view', $this->getRouterIdParameter().'/view');
  71. }
  72. }
  73. Removing a route
  74. --------------
  75. Extending ``Sonata\AdminBundle\Admin\Admin`` will give your Admin classes the following
  76. default routes:
  77. * batch
  78. * create
  79. * delete
  80. * export
  81. * edit
  82. * list
  83. * show
  84. You can view all of the current routes defined for an Admin class by using the console to run
  85. ``php app/console sonata:admin:explain <<admin.service.name>>``
  86. for example if your Admin is called sonata.admin.foo you would run
  87. ``php app/console sonata:admin:explain sonata.admin.foo``
  88. Sonata internally checks for the existence of a route before linking to it. As a result, removing a
  89. route will prevent links to that action from appearing in the administrative interface. For example,
  90. removing the 'create' route will prevent any links to "Add new" from appearing.
  91. Removing a single route
  92. ^^^^^^^^^^^^^^^^^^^^^^^
  93. Any single registered route can be easily removed by name:
  94. .. code-block:: php
  95. <?php
  96. use Sonata\AdminBundle\Route\RouteCollection;
  97. class MediaAdmin extends Admin
  98. {
  99. protected function configureRoutes(RouteCollection $collection)
  100. {
  101. $collection->remove('delete');
  102. }
  103. }
  104. Removing all routes except named ones
  105. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  106. If you want to disable all default Sonata routes except few whitelisted ones, you can use
  107. the ``clearExcept()`` method. This method accepts an array of routes you want to keep active.
  108. .. code-block:: php
  109. <?php
  110. use Sonata\AdminBundle\Route\RouteCollection;
  111. class MediaAdmin extends Admin
  112. {
  113. protected function configureRoutes(RouteCollection $collection)
  114. {
  115. //Only `list` and `edit` route will be active
  116. $collection->clearExcept(array('list', 'edit'));
  117. }
  118. }
  119. Removing all routes
  120. ^^^^^^^^^^^^^^^^^^^
  121. If you want to remove all default routes, you can use ``clear()`` method.
  122. .. code-block:: php
  123. <?php
  124. use Sonata\AdminBundle\Route\RouteCollection;
  125. class MediaAdmin extends Admin
  126. {
  127. protected function configureRoutes(RouteCollection $collection)
  128. {
  129. //All routes are removed
  130. $collection->clear();
  131. }
  132. }
  133. Removing routes only when an Admin is embedded
  134. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  135. To prevent some routes from being available when one Admin is embedded inside another one
  136. (e.g. to remove the "add new" option when you embed ``TagAdmin`` within ``PostAdmin``) you
  137. can use ``hasParentFieldDescription()`` to detect this case and remove the routes.
  138. .. code-block:: php
  139. <?php
  140. use Sonata\AdminBundle\Route\RouteCollection;
  141. class TagAdmin extends Admin
  142. {
  143. protected function configureRoutes(RouteCollection $collection)
  144. {
  145. if($this->hasParentFieldDescription()) { // prevent display of "Add new" when embedding this form
  146. $collection->remove('create');
  147. }
  148. }
  149. }
  150. Persistent parameters
  151. ---------------------
  152. In some cases, the interface might be required to pass the same parameters
  153. across the different ``Admin``'s actions. Instead of setting them in the
  154. template or doing other weird hacks, you can define a ``getPersistentParameters``
  155. method. This method will be used when a link is being generated.
  156. .. code-block:: php
  157. <?php
  158. class MediaAdmin extends Admin
  159. {
  160. public function getPersistentParameters()
  161. {
  162. if (!$this->getRequest()) {
  163. return array();
  164. }
  165. return array(
  166. 'provider' => $this->getRequest()->get('provider'),
  167. 'context' => $this->getRequest()->get('context', 'default'),
  168. );
  169. }
  170. }
  171. // the result:
  172. // $admin->generateUrl('create') => /admin/module/create?context=default
  173. Changing the default route in a List Action
  174. -------------------------------------------
  175. Usually the identifier column of a list action links to the edit screen. To change the
  176. list action's links to point to a different action, set the ``route`` option in your call to
  177. ``ListMapper::addIdentifier()``. For example, to link to show instead of edit:
  178. .. code-block:: php
  179. <?php
  180. class PostAdmin extends Admin
  181. {
  182. public function configureListFields(ListMapper $listMapper)
  183. {
  184. $listMapper
  185. ->addIdentifier('name', null, array(
  186. 'route' => array('name' => 'show')
  187. ));
  188. }
  189. }