浏览代码

simplify urls definition, add renderJson into the CRUD controller

Thomas 14 年之前
父节点
当前提交
25117d41a6
共有 3 个文件被更改,包括 19 次插入3 次删除
  1. 11 0
      Controller/CRUDController.php
  2. 0 1
      Resources/views/CRUD/base_edit.twig.html
  3. 8 2
      Route/AdminPoolLoader.php

+ 11 - 0
Controller/CRUDController.php

@@ -23,8 +23,19 @@ use Sonata\BaseApplicationBundle\Tool\DoctrinePager as Pager;
 class CRUDController extends Controller
 {
 
+
     protected $admin;
 
+    public function renderJson($data)
+    {
+        $response = new \Symfony\Component\HttpFoundation\Response;
+        $response->setContent(json_encode($data));
+        $response->headers->set('Content-Type', 'application/json');
+//        $response->headers->set('Content-Type', 'text/plain');
+
+        return $response;
+    }
+
     /**
      * Sets the Container associated with this Controller.
      *

+ 0 - 1
Resources/views/CRUD/base_edit.twig.html

@@ -23,7 +23,6 @@ file that was distributed with this source code.
 {% endblock %}
 
 {% block actions %}
-
     <div class="sonata-actions">
         <ul>
             <li class="sonata-action-element"><a href="{{ admin.generateUrl('create') }}">{% trans "link_action_create" from "BaseApplicationBundle" %}</a></li>

+ 8 - 2
Route/AdminPoolLoader.php

@@ -45,11 +45,17 @@ class AdminPoolLoader extends Loader
 
         $collection = new RouteCollection;
         foreach ($this->pool->getInstances() as $admin) {
-            foreach ($admin->getUrls() as $code => $configuration) {
+            foreach ($admin->getUrls() as $action => $configuration) {
 
+                $default = isset($configuration['defaults'])       ? $configuration['defaults'] : array();
+
+                if(!isset($default['_controller'])) {
+                    $default['_controller'] = sprintf('%s:%s', $admin->getBaseControllerName(), $action);
+                }
+                
                 $collection->add($configuration['name'], new Route(
                     $configuration['pattern'],
-                    isset($configuration['defaults'])       ? $configuration['defaults'] : array(),
+                    isset($configuration['defaults'])       ? $configuration['defaults'] : array('_controller'),
                     isset($configuration['requirements'])   ? $configuration['requirements'] : array(),
                     isset($configuration['options'])        ? $configuration['options'] : array()
                 ));