routing.rst 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. The same goes for the ``baseRoutePattern``.
  23. Routing usage
  24. -------------
  25. Inside a CRUD template, a route can be generated by using the ``Admin`` class.
  26. .. code-block:: html+jinja
  27. <a href="{{ admin.generateUrl('list') }}">List</a>
  28. <a href="{{ admin.generateUrl('list', params|merge('page': 1) }}">List</a>
  29. Create a route
  30. --------------
  31. You can easily register new routes by defining them in the ``Admin`` class.
  32. Only Admin routes should be registered this way. Of course this requires the
  33. related action to be defined in the controller.
  34. As route is always generated with the ``Admin`` context, it can only be
  35. defined by its name.
  36. .. code-block:: php
  37. <?php
  38. use Sonata\AdminBundle\Route\RouteCollection;
  39. class MediaAdmin extends Admin
  40. {
  41. protected function configureRoutes(RouteCollection $collection)
  42. {
  43. $collection->add('duplicate');
  44. $collection->add('view', $this->getRouterIdParameter().'/view');
  45. }
  46. }
  47. Removing a route
  48. --------------
  49. Any registered route can be easily removed.
  50. .. code-block:: php
  51. <?php
  52. use Sonata\AdminBundle\Route\RouteCollection;
  53. class MediaAdmin extends Admin
  54. {
  55. protected function configureRoutes(RouteCollection $collection)
  56. {
  57. $collection->remove('delete');
  58. }
  59. }
  60. If you want to disable all default Sonata routes except few whitelisted ones, you can use ``clearExcept()`` method.
  61. This method accepts an array of routes you want to keep active.
  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. //Only `list` and `edit` route will be active
  70. $collection->clearExcept(array('list', 'edit'));
  71. }
  72. }
  73. If you want to remove all default routes, you can use ``clear()`` method.
  74. .. code-block:: php
  75. <?php
  76. use Sonata\AdminBundle\Route\RouteCollection;
  77. class MediaAdmin extends Admin
  78. {
  79. protected function configureRoutes(RouteCollection $collection)
  80. {
  81. //All routes are removed
  82. $collection->clear();
  83. }
  84. }
  85. Persistent parameters
  86. ---------------------
  87. In some cases, the interface might be required to pass the same parameters
  88. across the different ``Admin``'s actions. Instead of setting them in the
  89. template or doing other weird hacks, you can define a ``getPersistentParameters``
  90. method. This method will be used when a link is being generated.
  91. .. code-block:: php
  92. <?php
  93. class MediaAdmin extends Admin
  94. {
  95. public function getPersistentParameters()
  96. {
  97. if (!$this->getRequest()) {
  98. return array();
  99. }
  100. return array(
  101. 'provider' => $this->getRequest()->get('provider'),
  102. 'context' => $this->getRequest()->get('context', 'default'),
  103. );
  104. }
  105. }
  106. // the result :
  107. // $admin->generateUrl('create') => /admin/module/create?context=default