Selaa lähdekoodia

Added details about how to set up a new action in routing.rst, part of #1525
Also some minor edits for readability

Christian Morgan 11 vuotta sitten
vanhempi
commit
be3958d46b
1 muutettua tiedostoa jossa 46 lisäystä ja 4 poistoa
  1. 46 4
      Resources/doc/reference/routing.rst

+ 46 - 4
Resources/doc/reference/routing.rst

@@ -62,7 +62,8 @@ You will then have route URLs like ``http://yourdomain.com/admin/foo/list`` and
 Routing usage
 -------------
 
-Inside a CRUD template, a route can be generated by using the ``Admin`` class.
+Inside a CRUD template, a route for the current ``Admin`` class can be generated via
+the admin variable's ``generateUrl()`` command:
 
 .. code-block:: html+jinja
 
@@ -78,8 +79,7 @@ Create a route
 --------------
 
 You can register new routes by defining them in your ``Admin`` class. Only Admin 
-routes should be registered this way. Of course this requires the related action 
-to be defined in the controller.
+routes should be registered this way.
 
 The routes you define in this way are generated within your Admin's context, and
 the only required parameter to ``add()`` is the action name. The second parameter
@@ -95,11 +95,53 @@ explicitly this defaults to the action name.
     {
         protected function configureRoutes(RouteCollection $collection)
         {
-            $collection->add('duplicate');
+            $collection->add('myCustomAction');
             $collection->add('view', $this->getRouterIdParameter().'/view');
         }
     }
 
+Other steps needed to create your new action
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+In addition to defining the route for your new action you also need to create a
+handler for it in your Controller. By default Admin classes use ``SonataAdminBundle:CRUD``
+as their controller, but this can be changed by altering the third argument when defining
+your Admin service (in your admin.yml file).
+
+For example, lets change the Controller for our MediaAdmin class to AcmeDemoBundle:MediaCRUD:
+
+.. code-block:: jinja
+
+    # src/Acme/DemoBundle/Resources/config/admin.yml
+    sonata.admin.media:
+        class: Acme\DemoBundle\Admin\MediaAdmin
+        tags:
+            - { name: sonata.admin, manager_type: orm, label: "Media" }
+        arguments:
+            - ~
+            - Acme\DemoBundle\Entity\Page
+            - 'AcmeDemoBundle:MediaCRUD' # define the new controller via the third argument
+        calls:
+            - [ setTranslationDomain, [Acme\DemoBundle]]
+
+We now need to create our Controller, the easiest way is to extend the basic Sonata CRUD controller:
+
+.. code-block:: php
+
+    <?php
+    // /src/Acme/DemoBundle/Controller/MediaCRUDController.php
+    namespace Acme\DemoBundle\Controller;
+
+    use Sonata\AdminBundle\Controller\CRUDController;
+
+    class MediaCRUDController extends CRUDController
+    {
+        public function myCustomAction()
+        {
+            // your code here ...
+        }
+    }
+
 Removing a route
 --------------