瀏覽代碼

Merge remote-tracking branch 'pulse00/master'

Jordi Boggiano 12 年之前
父節點
當前提交
f1e6372194
共有 1 個文件被更改,包括 22 次插入1 次删除
  1. 22 1
      src/Packagist/WebBundle/Controller/WebController.php

+ 22 - 1
src/Packagist/WebBundle/Controller/WebController.php

@@ -94,7 +94,7 @@ class WebController extends Controller
     }
 
     /**
-     * @Route("/search/", name="search")
+     * @Route("/search.{_format}", requirements={"_format"="(html|json)"}, name="search")
      */
     public function searchAction(Request $req)
     {
@@ -123,6 +123,27 @@ class WebController extends Controller
                 $paginator = new Pagerfanta(new SolariumAdapter($solarium, $select));
                 $paginator->setMaxPerPage(15);
                 $paginator->setCurrentPage($req->query->get('page', 1), false, true);
+                
+                if ($req->getRequestFormat() === 'json') {
+                    
+                    $response = new Response();
+                    $result = array();
+
+                    foreach ($paginator as $package) {
+                        $url = $this->get('router')->generate('view_package', array(
+                            'name' => $package->name, 
+                            '_format' => 'json'
+                        ), true);
+                        
+                        $result[] = array(
+                            'name' => $package->name,
+                            'description' => $package->description ?: '',
+                            'url' => $url   
+                        );
+                    }
+                    $response->setContent(json_encode($result));
+                    return $response;
+                }
 
                 if ($req->isXmlHttpRequest()) {
                     return $this->render('PackagistWebBundle:Web:list.html.twig', array(