routing.rst 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. You can set a ``baseRouteName`` property inside your ``Admin`` class, which
  11. represents the route prefix.
  12. .. code-block:: php
  13. <?php
  14. class PostAdmin extends Admin
  15. {
  16. protected $baseRouteName = 'news_post_admin';
  17. }
  18. If no ``baseRouteName`` is defined then the Admin will pick one for you, built
  19. in the following format: 'admin_vendor_bundlename_entityname_action'. If the
  20. Admin fails to find the best baseRouteName then a ``RuntimeException`` will
  21. be thrown.
  22. You can also use ``baseRoutePattern`` to set a custom URL for a given ``Admin`` class.
  23. For example, to use ``http://yourdomain.com/admin/foo`` as the base URL for
  24. the ``FooAdmin`` class (instead of the default of ``http://yourdomain.com/admin/vendor/bundle/foo``)
  25. use the following code:
  26. .. code-block:: php
  27. <?php
  28. class FooAdmin extends Admin
  29. {
  30. protected $baseRouteName = 'foo';
  31. }
  32. You will then have route URLs like ``http://yourdomain.com/admin/foo/list`` and
  33. ``http://yourdomain.com/admin/foo/1/edit``
  34. Routing usage
  35. -------------
  36. Inside a CRUD template, a route can be generated by using the ``Admin`` class.
  37. .. code-block:: html+jinja
  38. <a href="{{ admin.generateUrl('list') }}">List</a>
  39. <a href="{{ admin.generateUrl('list', params|merge('page': 1) }}">List</a>
  40. Create a route
  41. --------------
  42. You can easily register new routes by defining them in the ``Admin`` class.
  43. Only Admin routes should be registered this way. Of course this requires the
  44. related action to be defined in the controller.
  45. As route is always generated with the ``Admin`` context, it can only be
  46. defined by its name.
  47. .. code-block:: php
  48. <?php
  49. use Sonata\AdminBundle\Route\RouteCollection;
  50. class MediaAdmin extends Admin
  51. {
  52. protected function configureRoutes(RouteCollection $collection)
  53. {
  54. $collection->add('duplicate');
  55. $collection->add('view', $this->getRouterIdParameter().'/view');
  56. }
  57. }
  58. Removing a route
  59. --------------
  60. Any registered route can be easily removed.
  61. .. code-block:: php
  62. <?php
  63. use Sonata\AdminBundle\Route\RouteCollection;
  64. class MediaAdmin extends Admin
  65. {
  66. protected function configureRoutes(RouteCollection $collection)
  67. {
  68. $collection->remove('delete');
  69. }
  70. }
  71. If you want to disable all default Sonata routes except few whitelisted ones, you can use ``clearExcept()`` method.
  72. This method accepts an array of routes you want to keep active.
  73. .. code-block:: php
  74. <?php
  75. use Sonata\AdminBundle\Route\RouteCollection;
  76. class MediaAdmin extends Admin
  77. {
  78. protected function configureRoutes(RouteCollection $collection)
  79. {
  80. //Only `list` and `edit` route will be active
  81. $collection->clearExcept(array('list', 'edit'));
  82. }
  83. }
  84. If you want to remove all default routes, you can use ``clear()`` method.
  85. .. code-block:: php
  86. <?php
  87. use Sonata\AdminBundle\Route\RouteCollection;
  88. class MediaAdmin extends Admin
  89. {
  90. protected function configureRoutes(RouteCollection $collection)
  91. {
  92. //All routes are removed
  93. $collection->clear();
  94. }
  95. }
  96. To prevent some routes from being available when one Admin is embedded inside another one
  97. (e.g. to remove the "add new" option when you embed ``TagAdmin`` within ``PostAdmin``) you
  98. can use ``hasParentFieldDescription()`` to detect this case and remove the routes.
  99. .. code-block:: php
  100. <?php
  101. use Sonata\AdminBundle\Route\RouteCollection;
  102. class TagAdmin extends Admin
  103. {
  104. protected function configureRoutes(RouteCollection $collection)
  105. {
  106. if($this->hasParentFieldDescription()) { // prevent display of "Add new" when embedding this form
  107. $collection->remove('create');
  108. }
  109. }
  110. }
  111. Persistent parameters
  112. ---------------------
  113. In some cases, the interface might be required to pass the same parameters
  114. across the different ``Admin``'s actions. Instead of setting them in the
  115. template or doing other weird hacks, you can define a ``getPersistentParameters``
  116. method. This method will be used when a link is being generated.
  117. .. code-block:: php
  118. <?php
  119. class MediaAdmin extends Admin
  120. {
  121. public function getPersistentParameters()
  122. {
  123. if (!$this->getRequest()) {
  124. return array();
  125. }
  126. return array(
  127. 'provider' => $this->getRequest()->get('provider'),
  128. 'context' => $this->getRequest()->get('context', 'default'),
  129. );
  130. }
  131. }
  132. // the result :
  133. // $admin->generateUrl('create') => /admin/module/create?context=default