我使用FOSuserbundle上手的用戶註冊https://github.com/FriendsOfSymfony/FOSUserBundle訪問登錄的用戶在模板
我知道了註冊/登錄和註銷。我現在要做的是抓取登錄的用戶數據並將其顯示在我網站的每個頁面上。像「Hi用戶名」在標題類型的東西。
好像在我應用程序/資源/視圖/ base.html.twig嵌入控制器做到這一點http://symfony.com/doc/current/book/templating.html#embedding-controllers
所以我寫了我的控制器來訪問用戶配置文件數據的最佳方式。我想不出的是如何在我的嵌入式控制器中訪問FOS方法。所以從我的阿克米/ UserBundle /控制器/ UserController.php我想這樣做:
public function showAction()
{
$user = $this->container->get('security.context')->getToken()->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException(
'This user does not have access to this section.');
}
return $this->container->get('templating')
->renderResponse('FOSUserBundle:Profile:show.html.'.$this->container
->getParameter('fos_user.template.engine'), array('user' => $user));
}
這是我從抓起: 供應商/包/ FOS/UserBundle /控制器/ ProfileController.php
謝謝。我實際上已將「ROLE」更改爲「IS_AUTHENTICATED_REMEMBERED」,並且效果很好。 – ed209
我沒有使用FOSuserbundle(除非它現在帶有),但'app.user.username'仍然適用於我。可能對有人知道有用。 –
謝謝!在文檔中這應該更清晰。 –