2017-02-11 64 views
1

我正在使用symfony 3進行項目工作,並且我想通過他們正在處理的項目的名稱動態獲取數據庫中的用戶列表。如何使用Symfony 3在樹枝中包含AJAX

這是控制器的動作: enter image description here

這樣,它不會顯示任何東西,那隻能說明第一個用戶,如果我寫的操作是這樣的: enter image description here

+0

我會先用調試器中運行整個事情,做一個突破點你得到你的結果,並檢查你在$膿中得到了什麼。如果你不能運行調試器,只需在foreach之前插入一些var_dump($ pus)即可。這會給你一個線索的問題。如果不知道你的ProjectUser結構,或者這個findByName實際上是一個本地存儲庫函數或者你寫的東西,這很難說。無論如何,LeBlobb在他的回答中提到的將會更符合Symfony中的預期 - 而不是在控制器中生成HTML。 – userfuser

+0

問題出在我修復它們的存儲庫查詢中,但現在foreach循環不返回任何內容。如果我在循環之外使用它,任何構建在foreach中的數組都被標記爲undefined。 –

+0

這不應該。你確定你正在構建陣列嗎,或者甚至進入這個習慣?你打開了錯誤報告嗎?也許有什麼事情發生,你沒有看到報道。你在開發或生產模式? – userfuser

回答

1

如果我不明白對,你有一個頁面,你有一個AJAX請求,它將返回已經用樹枝渲染的一些數據。

你需要做的是:

返回正常的HTML頁面

  • 第二控制器返回您呈現的數據與您的第一個控制器的做法,但

    1. 一個控制器,這一個使用其中只包含AJAX請求將返回的HTML模板。

    2. 在您的AJAX請求中調用第二個控制器中定義的路由並顯示結果。

    例1:

    ControllerOne.php:

    class ControllerOne extends Controller 
    { 
        /** 
        * @Route("/") 
        * @return \Symfony\Component\HttpFoundation\Response 
        */ 
        public function indexAction() 
        { 
         // show your page 
         return $this->render('index.html.twig'); 
        } 
    } 
    

    ControllerTwo.php:

    class ControllerTwo extends Controller 
    { 
        /** 
        * @Route("get/data/{userId}") 
        * @param $userId 
        * @return \Symfony\Component\HttpFoundation\Response 
        */ 
        public function getDataAction($userId) 
        { 
         // get some data 
         $em = $this->getDoctrine()->getManager(); 
    
         // this example shows retrieving user data 
         // implement your logic for retrieving projecty by user id here 
         $userData = $em->getRepository('AppBundle:User')->findOneBy(array('id' => $userId)); 
    
         return $this->render('user.data.html.twig', array('user' => $userData)); 
        } 
    } 
    

    index.html.twig:

    <!DOCTYPE html> 
    <html lang="en"> 
    <head> 
        <meta charset="UTF-8"> 
        <title>Title</title> 
    </head> 
    <body> 
        <script type="application/javascript"> 
    
         // call the controller to get the rendered data 
         $.ajax({ 
          url: "/get/data/1" 
         }) 
           .done(function(data) { 
            // insert data into div 
            $("#myDiv").html(data); 
           }); 
        </script> 
    </body> 
    </html> 
    

    user.data.html.twig:

    <div> 
        <p> 
         <!-- you have access to the data passed from controller here --> 
         User name: {{user.name}} 
        </p> 
    </div> 
    

    例2:

    JsonResponseController.php:

    class JsonResponseController extends Controller 
    { 
        /** 
        * @Route("get/data/{userId}") 
        * @param $userId 
        * @return \Symfony\Component\HttpFoundation\Response 
        */ 
        public function getDataAction($userId) 
        { 
         // get some data 
         $em = $this->getDoctrine()->getManager(); 
    
         // this example shows retrieving user data 
         // implement your logic for retrieving projecty by user id here 
         $userData = $em->getRepository('AppBundle:User')->findOneBy(array('id' => $userId)); 
    
         return new JsonResponse(array('userData' => $userData)); 
        } 
    } 
    
  • +0

    我沒有其他的視圖來呈現我只想讓用戶的名字顯示在

    +0

    然後,您只需在檢索數據的操作中返回一個Symfony \ Component \ HttpFoundation \ JsonResponse:http ://symfony.com/doc/current/components/http_foundation.html#creating-a-json-response如果有幫助,虐待我的答案,所以你可以接受它 – LeBlobb

    +0

    我又增加了一個例子 – LeBlobb