123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- Routing
- =======
- The default routes used in the CRUD controller are accessible through the
- ``Admin`` class.
- The ``Admin`` class contains two routing methods:
- * ``getRoutes()``: Returns the available routes;
- * ``generateUrl($name, $options)``: Generates the related routes.
- Routing Definition
- ------------------
- You can set a ``baseRouteName`` property inside your ``Admin`` class, which
- represents the route prefix.
- .. code-block:: php
- <?php
- class PostAdmin extends Admin
- {
- protected $baseRouteName = 'news_post_admin';
- }
- If no ``baseRouteName`` is defined then the Admin will pick one for you, built
- in the following format: 'admin_vendor_bundlename_entityname_action'. If the
- Admin fails to find the best baseRouteName then a ``RuntimeException`` will
- be thrown.
- The same goes for the ``baseRoutePattern``.
- Routing usage
- -------------
- Inside a CRUD template, a route can be generated by using the ``Admin`` class.
- .. code-block:: html+jinja
- <a href="{{ admin.generateUrl('list') }}">List</a>
- <a href="{{ admin.generateUrl('list', params|merge('page': 1) }}">List</a>
- Create a route
- --------------
- You can easily register new routes by defining them in the ``Admin`` class.
- Only Admin routes should be registered this way. Of course this requires the
- related action to be defined in the controller.
- As route is always generated with the ``Admin`` context, it can only be
- defined by its name.
- .. code-block:: php
- <?php
- use Sonata\AdminBundle\Route\RouteCollection;
- class MediaAdmin extends Admin
- {
- protected function configureRoutes(RouteCollection $collection)
- {
- $collection->add('duplicate');
- $collection->add('view', $this->getRouterIdParameter().'/view');
- }
- }
- Removing a route
- --------------
- Any registered route can be easily removed.
- .. code-block:: php
- <?php
- use Sonata\AdminBundle\Route\RouteCollection;
- class MediaAdmin extends Admin
- {
- protected function configureRoutes(RouteCollection $collection)
- {
- $collection->remove('delete');
- }
- }
- If you want to disable all default Sonata routes except few whitelisted ones, you can use ``clearExcept()`` method.
- This method accepts an array of routes you want to keep active.
- .. code-block:: php
- <?php
- use Sonata\AdminBundle\Route\RouteCollection;
- class MediaAdmin extends Admin
- {
- protected function configureRoutes(RouteCollection $collection)
- {
- //Only `list` and `edit` route will be active
- $collection->clearExcept(array('list', 'edit'));
- }
- }
- If you want to remove all default routes, you can use ``clear()`` method.
- .. code-block:: php
- <?php
- use Sonata\AdminBundle\Route\RouteCollection;
- class MediaAdmin extends Admin
- {
- protected function configureRoutes(RouteCollection $collection)
- {
- //All routes are removed
- $collection->clear();
- }
- }
- Persistent parameters
- ---------------------
- In some cases, the interface might be required to pass the same parameters
- across the different ``Admin``'s actions. Instead of setting them in the
- template or doing other weird hacks, you can define a ``getPersistentParameters``
- method. This method will be used when a link is being generated.
- .. code-block:: php
- <?php
- class MediaAdmin extends Admin
- {
- public function getPersistentParameters()
- {
- if (!$this->getRequest()) {
- return array();
- }
- return array(
- 'provider' => $this->getRequest()->get('provider'),
- 'context' => $this->getRequest()->get('context', 'default'),
- );
- }
- }
- // the result :
- // $admin->generateUrl('create') => /admin/module/create?context=default
|