Search code examples
symfonyfosuserbundle

FOSuserBundle override controller


What is the right way to pass more variables to FOSUserBundle settings twig template (Profile/show_content.html.twig) in Symfony 3.4?

I basically want to rewrite showAction() method and pass more than user variable ti twig template.

I tried to following this tutorial. It seems it does no longer work with Symfony 3.4


Solution

  • The way I do it (and there might be better methods) is simply create a new controller with a route to the original 'show route', together with the variables I want to pass. Here is an example of the showAction() with an extra variable 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
        ));
        }
    }