在FOSUserBundle中从登录的用户中获取用户ID[英] Getting the user id from logged in user in FOSUserBundle

本文是小编为大家收集整理的关于在FOSUserBundle中从登录的用户中获取用户ID的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试创建一个任务管理器,我需要能够获取当前登录的用户的用户ID,以便在创建任务时也将输入用户ID输入数据库.我无法让它工作,它一直告诉我call to undefined function getUser()我不认为我必须定义它,我认为它是内置的.

有人可以帮助我解决这个问题,这将不胜感激!

这是创建任务的函数

<?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(),
        );
    }

推荐答案

您不需要创建新的用户对象$user = new User();,您可以从安全上下文中登录的用户对象

登录的当前
$user = $this->container->get('security.token_storage')->getToken()->getUser();
$user->getId();

编辑: security.context已弃用,而是使用security.token_storage.有关更多详细信息: https://symmfony.com/博客/新符合2-6-Security-component-Improvements

其他推荐答案

简单地写

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

您获得当前用户的ID

其他推荐答案

这对我有用 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();
}

本文地址:https://www.itbaoku.cn/post/2090750.html

问题描述

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(),
        );
    }

推荐答案

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

其他推荐答案

simply writing

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

You get the Id of the current user

其他推荐答案

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();
}