routing.rst 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 method:
  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 on for you, built on
  18. following format : 'admin_vendor_bundlename_entityname_action'. If the Admin
  19. fails to find the best baseRouteName then a ``RuntimeException`` will
  20. be throw.
  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. Only Admin routes should be register this
  31. way. Of course this require to have the related action defined in the controller.
  32. A route is always generated with the ``Admin`` context, that is why a route can be only defined by its name.
  33. .. code-block:: php
  34. use Sonata\AdminBundle\Route\RouteCollection;
  35. class MediaAdmin extends Admin
  36. {
  37. public function configureRoutes(RouteCollection $collection)
  38. {
  39. $collection->add('duplicate');
  40. $collection->add('view', $this->getRouterIdParameter().'/view');
  41. }
  42. }
  43. Persistent parameters
  44. ---------------------
  45. In some cases, the interface might required to pass the same parameters across the different ``Admin``'s actions.
  46. Instead of settings them in the template or doing other weird hacks, you can defined a ``getPersistentParameters``
  47. method. This method will be used when a link is being generated.
  48. .. code-block:: php
  49. class MediaAdmin extends Admin
  50. {
  51. public function getPersistentParameters()
  52. {
  53. if(!$this->getRequest()) {
  54. return array();
  55. }
  56. return array(
  57. 'provider' => $this->getRequest()->get('provider'),
  58. 'context' => $this->getRequest()->get('context', 'default'),
  59. );
  60. }
  61. }
  62. // the result :
  63. // $admin->generateUrl('create') => /admin/module/create?context=default