10

I am trying to create a task manager and I need to be able to get the user id of the user that is currently logged in so that when the task is created the user id is entered into the database as well. I can not get it to work it keeps telling me that the call to undefined function getUser() i did not think that I had to define it, I thought it was built in.

Can someone help me fix this it would be much appreciated!

here is the function that creates the task

<?php

namespace Starnes\TaskBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Starnes\TaskBundle\Entity\Task;
use Starnes\TaskBundle\Form\TaskType;
use Starnes\UserBundle\Entity\User;

/**
 * Task controller.
 *
 * @Route("/task")
 */
class TaskController extends Controller
{

    /**
     * Lists all Task entities.
     *
     * @Route("/", name="task")
     * @Method("GET")
     * @Template()
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('StarnesTaskBundle:Task')->findAll();

        return array(
            'entities' => $entities,
        );
    }
    /**
     * Creates a new Task entity.
     *
     * @Route("/", name="task_create")
     * @Method("POST")
     * @Template("StarnesTaskBundle:Task:new.html.twig")
     * 
     */
    public function createAction(Request $request)
    {
        $user = new User();
        $userID = $user->getUser()->getId();
        $entity = new Task();
        $form = $this->createCreateForm($entity);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $entity->setUserId($userID);
            $em->flush();

            return $this->redirect($this->generateUrl('task_show', array('id' => $entity->getId())));
        }

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
Zach Starnes
  • 3,108
  • 9
  • 39
  • 63

4 Answers4

20

You don't need to create new user object $user = new User(); ,you can the current logged in user object from security context

$user = $this->container->get('security.token_storage')->getToken()->getUser();
$user->getId();

Edit: security.context is deprecated, use security.token_storage instead. For more details : https://symfony.com/blog/new-in-symfony-2-6-security-component-improvements

abdusco
  • 9,700
  • 2
  • 27
  • 44
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
  • 2
    it didn't work for me. i got error: You have requested a non-existent service "security.context". – Dev M Sep 15 '17 at 08:25
20

simply writing

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

You get the Id of the current user

ioses
  • 997
  • 1
  • 8
  • 11
5

This is working for me with Symfony 3

if( $this->container->get( 'security.authorization_checker' )->isGranted( 'IS_AUTHENTICATED_FULLY' ) )
{
    $user = $this->container->get('security.token_storage')->getToken()->getUser();
    $username = $user->getUsername();
}
user2182349
  • 9,569
  • 3
  • 29
  • 41
0

you can just use this variable : {{ app.user.id }}.

It display the connected user id or nothing if no one is connected

M. Dao
  • 42
  • 1
  • 7