2017-06-01 55 views
-2

假設我們有控制器AdvertController.php與編輯操作public function editAction(Request $request)發送對象$user到視圖edit.html.twig$user包含許多屬性ANS方法返回一個字符串如何從控制器Symfony的發送對象視圖(小枝)3

public function editAction(Request $request){ 
$user = new User(); 
return $this->render('OCPlatformBundle:Advert:edit.html.twig', array()); 
} 

我需要在代碼中添加什麼?因此,在視圖中,我可以顯示值

edit.html.twig 

{{ user.name }} 

回答

0

它在您已經定義的數組()中。

$x = 'someothervalue'; 
$this->render('template', [ 
    'somekey' => 'somevalue', 
    'someotherkey' => $x 
]); 

然後在樹枝:

{{ somekey }}, {{ someotherkey }} 
+0

謝謝, 我在整個查詢中使用控制器類的這個方法只是一次正確的? –

+0

如果我想在模型中處理我的數據時發送其他內容 –

+0

不完全確定您對第一個問題的含義。但是你可以發送你喜歡的任何東西。 php進程不是異步的,所以當你編寫它時,所有的東西都按順序執行,所以你在渲染模板之前所做的任何更改都會包含在內。您最有可能想要選擇,而不是在編輯時創建用戶。 –

0

試試這個:

public function editAction(Request $request){ 
    $user = new User(); 
    return $this->render('OCPlatformBundle:Advert:edit.html.twig', 
     [ 
     'user' => $user, 
     ] 
    ); 
} 

在樹枝模板通話功能dump()和樹枝渲染所有new User()數據的:

{{ dump(user) }} 

可以讓他們像這樣:

{{ user.name }} 
{{ user.soname }} 
0

試試這個:

public function editAction(Request $request){ 
    $user = new User(); 

    return $this->redirect('https://symfony.com/doc/current/index.html'); 
} 
+0

c'est tres drole –

相關問題