php 从 FOSUserBundle 中的登录用户获取用户 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20911199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:40:06  来源:igfitidea点击:

Getting the user id from logged in user in FOSUserBundle

phpsymfonyfosuserbundle

提问by Zach Starnes

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.

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

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

回答by M Khalid Junaid

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

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

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

Edit:security.contextis deprecated, use security.token_storageinstead. For more details : https://symfony.com/blog/new-in-symfony-2-6-security-component-improvements

编辑:security.context已弃用,security.token_storage改为使用。更多详情:https: //symfony.com/blog/new-in-symfony-2-6-security-component-improvements

回答by ioses

simply writing

简单地写

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

You get the Id of the current user

你得到当前用户的Id

回答by user2182349

This is working for me with Symfony 3

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

回答by M. Dao

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

你可以只使用这个变量:{{ app.user.id }}。

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

如果没有连接,则显示连接的用户 ID 或不显示任何内容