routing.rst 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. class PostAdmin extends Admin
  14. {
  15. protected $baseRouteName = 'news_post_admin';
  16. }
  17. If no ``baseRouteName`` is defined then the Admin will pick one for you, built
  18. in the following format: 'admin_vendor_bundlename_entityname_action'. If the
  19. Admin fails to find the best baseRouteName then a ``RuntimeException`` will
  20. be thrown.
  21. The same goes for the ``baseRoutePattern``.
  22. Routing usage
  23. -------------
  24. Inside a CRUD template, a route can be generated by using the ``Admin`` class.
  25. .. code-block:: html
  26. <a href="{{ admin.generateUrl('list') }}">List</a>
  27. <a href="{{ admin.generateUrl('list', params|merge('page': 1) }}">List</a>
  28. Create a route
  29. --------------
  30. You can easily register new routes by defining them in the ``Admin`` class.
  31. Only Admin routes should be registered this way. Of course this requires the
  32. related action to be defined in the controller.
  33. As route is always generated with the ``Admin`` context, it can only be
  34. defined by its name.
  35. .. code-block:: php
  36. use Sonata\AdminBundle\Route\RouteCollection;
  37. class MediaAdmin extends Admin
  38. {
  39. public function configureRoutes(RouteCollection $collection)
  40. {
  41. $collection->add('duplicate');
  42. $collection->add('view', $this->getRouterIdParameter().'/view');
  43. }
  44. }
  45. Persistent parameters
  46. ---------------------
  47. In some cases, the interface might required to pass the same parameters across the different ``Admin``'s actions.
  48. Instead of settings them in the template or doing other weird hacks, you can defined a ``getPersistentParameters``
  49. method. This method will be used when a link is being generated.
  50. .. code-block:: php
  51. class MediaAdmin extends Admin
  52. {
  53. public function getPersistentParameters()
  54. {
  55. if (!$this->getRequest()) {
  56. return array();
  57. }
  58. return array(
  59. 'provider' => $this->getRequest()->get('provider'),
  60. 'context' => $this->getRequest()->get('context', 'default'),
  61. );
  62. }
  63. }
  64. // the result :
  65. // $admin->generateUrl('create') => /admin/module/create?context=default