FOSuserBundle覆盖控制器

时间:2018-07-31 08:49:55

标签: symfony fosuserbundle

在Symfony 3.4中将更多变量传递到FOSUserBundle设置树枝模板(Profile/show_content.html.twig)的正确方法是什么?

我基本上想重写showAction() method并传递多于user的变量t枝模板。

我尝试遵循此tutorial。看来它不再适用于Symfony 3.4

1 个答案:

答案 0 :(得分:1)

我这样做的方法(也许还有更好的方法)是简单地创建一个新的控制器,该控制器具有到原始“显示路由”的路由,以及要传递的变量。这是showAction()带有额外变量rendered_address的示例:

namespace App\Controller;

use FOS\UserBundle\Model\UserInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

class ProfileController extends Controller
{
    /**
    * Show the user.
    * @Route("/profile/show")
    */
    public function showAction()
    {
    $user = $this->getUser();
    if (!is_object($user) || !$user instanceof UserInterface) {
        throw new AccessDeniedException('This user does not have access to this section.');
    }

    $address = $this->getUser()->renderAddress(); // here is get my variable

    return $this->render('@FOSUser/Profile/show.html.twig', array(
        'user' => $user,
        'rendered_address' => $address // here is pass my variable
    ));
    }
}