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

编程入门 行业动态 更新时间:2024-10-27 07:22:07
本文介绍了从FOSUserBundle中的登录用户获取用户ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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!

这是创建任务的功能

<?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();,可以从安全上下文中访问当前登录的用户对象

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

:不建议使用security.context,请改用security.token_storage.有关更多详细信息: symfony/blog/new-in-symfony-2-6-security-component-improvements

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

更多推荐

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

本文发布于:2023-11-14 13:23:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587325.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用户   FOSUserBundle   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!