user3174311
user3174311

Reputation: 2003

FOSUserbundle access user from other controller

I'm using FOSUserbunble, how can access the user object from another controller/form in another bunble? for example I have a BlogBundle and when I create a new post I need to save the userId in the post table. for the form I'm using buildForm extending AbstractType.

thanks

Upvotes: 0

Views: 297

Answers (2)

Ajeet Varma
Ajeet Varma

Reputation: 726

You can do it via SecurityContext.
let's a example ..

  namespace YourPackage\UserBundle\Controller;

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

 class YourController extends Controller{
 /**
 * Fetch a user object.
 *
 * @Route("/yourRoute", name="your_route")
 * @Method()
 * @Template()
 */
public function yourAction(Request $request)
{
    $user = $this->container->get('security.context')->getToken()->getUser();


    return $this->redirect($this->generateUrl('your_route'));
}}

Upvotes: 0

geoB
geoB

Reputation: 4716

Try $user = $this->getUser();

Upvotes: 2

Related Questions