Php Symfony 4-FOSUserBundle-在自定义路由上呈现用户信息

Php Symfony 4-FOSUserBundle-在自定义路由上呈现用户信息,php,symfony,doctrine,fosuserbundle,symfony4,Php,Symfony,Doctrine,Fosuserbundle,Symfony4,我在自定义ProfileController中有一个构造函数和路由 private $userManager; public function __construct(UserManagerInterface $userManager) { $this->userManager = $userManager; } /** * @Route("/profile/bookings", name="profile_bookings") */ public function boo

我在自定义ProfileController中有一个构造函数和路由

private $userManager;

public function __construct(UserManagerInterface $userManager)
{
    $this->userManager = $userManager;
}

/**
 * @Route("/profile/bookings", name="profile_bookings")
 */
public function bookings()
{

    $user = $this->getUser();

    return $this->render('profile/bookings/bookings.html.twig', array('user'=>$user));
}
在我的模板中,我引用了

{{user.first_name}

但我得到了一个错误:

HTTP 500内部服务器错误 属性first\u name或方法first\u name、getfirst\u name/isfirst\u name/hasfirst\u name或u调用均不存在,并且在类App\Entity\User中具有公共访问权限

如何从数据库中获取用户信息并显示在配置文件的子页面中

编辑:用户实体

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;

/**
 * @ORM\Entity
 * @ORM\Table(name="`user`")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=190)
     */
    private $first_name;

    /**
     * @ORM\Column(type="string", length=190)
     */
    private $last_name;

    /**
     * @ORM\Column(type="string", length=190, nullable=true)
     */
    private $phone_number;

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $profile_height;

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $profile_weight;

    /**
     * @ORM\Column(type="date", nullable=true)
     */
    private $profile_dob;

    /**
     * @ORM\Column(type="string", length=190, nullable=true)
     */
    private $profile_gender;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Booking", mappedBy="user")
     */
    private $bookings;

    public function __construct()
    {
        parent::__construct();
        $this->bookings = new ArrayCollection();
    }

    /**
     * Overridde setEmail method so that username is now optional
     *
     * @param string $email
     * @return User
     */
    public function setEmail($email)
    {
        $this->setUsername($email);

        return parent::setEmail($email);
    }

    public function getFirstName()
    {
        return $this->first_name;
    }

    public function setFirstName($first_name)
    {
        $this->first_name = $first_name;
    }

    public function getLastName()
    {
        return $this->last_name;
    }

    public function setLastName($last_name)
    {
        $this->last_name = $last_name;
    }

    public function getPhoneNumber(): ?string
    {
        return $this->phone_number;
    }

    public function setPhoneNumber(string $phone_number): self
    {
        $this->phone_number = $phone_number;

        return $this;
    }

    public function getProfileHeight(): ?int
    {
        return $this->profile_height;
    }

    public function setProfileHeight(?int $profile_height): self
    {
        $this->profile_height = $profile_height;

        return $this;
    }

    public function getProfileDob(): ?\DateTimeInterface
    {
        return $this->profile_dob;
    }

    public function setProfileDob(?\DateTimeInterface $profile_dob): self
    {
        $this->profile_dob = $profile_dob;

        return $this;
    }

    public function getProfileWeight(): ?int
    {
        return $this->profile_weight;
    }

    public function setProfileWeight(?int $profile_weight): self
    {
        $this->profile_weight = $profile_weight;

        return $this;
    }

    public function getProfileGender(): ?string
    {
        return $this->profile_gender;
    }

    public function setProfileGender(?string $profile_gender): self
    {
        $this->profile_gender = $profile_gender;

        return $this;
    }

    /**
     * @return Collection|Booking[]
     */
    public function getBookings(): Collection
    {
        return $this->bookings;
    }

    public function addBooking(Booking $booking): self
    {
        if (!$this->bookings->contains($booking)) {
            $this->bookings[] = $booking;
            $booking->setUser($this);
        }

        return $this;
    }

    public function removeBooking(Booking $booking): self
    {
        if ($this->bookings->contains($booking)) {
            $this->bookings->removeElement($booking);
            // set the owning side to null (unless already changed)
            if ($booking->getUser() === $this) {
                $booking->setUser(null);
            }
        }

        return $this;
    }

}

谢谢。

只需在您的小树枝模板中使用:

{{ user.getFirstName }}
它很好用。通常,Twig在PHP层所做的工作非常简单:


顺便说一下,您应该遵循变量的,因为twig很难找到用snake_case编写的属性值。

只需在twig模板中使用:

{{ user.getFirstName }}
它很好用。通常,Twig在PHP层所做的工作非常简单:


顺便说一下,您应该遵循变量的,因为twig很难找到用snake_case编写的属性的值。

我认为您不应该在控制器中构造UserManagerInterface。另外,正如Franck所说,如果可以,使用编码标准,它将在将来节省大量时间和挫折

以下是我在Symfony 4项目中使用的控制器:

namespace App\Controller;

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

/**
 * @Route("/profile/bookings", name="profile_bookings")
 */
public function bookings()
{
    $user = $this->getUser();
    if (!is_object($user) || !$user instanceof UserInterface) {
        throw new AccessDeniedException('This user does not have access to this section.');
    }

    return $this->render('profile/bookings/bookings.html.twig', array(
        'user' => $user,
    ));
    }
} 

我认为您不应该在控制器中构造UserManagerInterface。另外,正如Franck所说,如果可以,使用编码标准,它将在将来节省大量时间和挫折

以下是我在Symfony 4项目中使用的控制器:

namespace App\Controller;

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

/**
 * @Route("/profile/bookings", name="profile_bookings")
 */
public function bookings()
{
    $user = $this->getUser();
    if (!is_object($user) || !$user instanceof UserInterface) {
        throw new AccessDeniedException('This user does not have access to this section.');
    }

    return $this->render('profile/bookings/bookings.html.twig', array(
        'user' => $user,
    ));
    }
} 

@Franck Gamess是对的,但你也可以摆脱get。 如果您编写{{user.firstName}},twig将自动将其与方法getFirstName关联


我不知道你为什么要用snake_case来写你的属性,但是你可以把它改成camelCase,并通过它们的真名来访问你的属性。

@Franck Gamess是对的,但你也可以摆脱get。 如果您编写{{user.firstName}},twig将自动将其与方法getFirstName关联


我不知道你为什么要用snake_case编写你的属性,但你可以将其更改为camelCase,并通过真实名称访问你的属性。

你能显示你的用户实体吗?是的,刚刚添加了它。谢谢。你能显示你的用户实体吗?是的,刚刚添加。谢谢。关于蛇案的好建议,谢谢。等等,所以我应该在我的twig中使用方法而不是变量?如果您使用像my_variable这样的变量,那么像这样访问值会更容易,因为您必须为getter/setter使用一个奇怪的名称才能访问twig中的这个私有属性。或者使用myVariableGood advice之类的变量,谢谢。等等,所以我应该在我的twig中使用方法而不是变量?如果您使用像my_variable这样的变量,那么像这样访问值会更容易,因为您必须为getter/setter使用一个奇怪的名称才能访问twig中的这个私有属性。或者使用像myVariableI这样的变量当你说他不应该在控制器中构造UserManager时,我并不反对你。在许多情况下,在我的控制器上,我使用了一些经理/服务的方法,因此我将其注入到_构造中,并可以在整个控制器中访问它。@Etshy,我认为既然symfony 3.3,就应该使用依赖注入。我错了吗?是的,这就是他对服务类型暗示所做的。它将服务注入控制器的构造函数中以便使用。当你说他不应该在控制器中构造UserManager时,我并不反对你。在许多情况下,在我的控制器上,我使用了一些经理/服务的方法,因此我将其注入到_构造中,并可以在整个控制器中访问它。@Etshy,我认为既然symfony 3.3,就应该使用依赖注入。我错了吗?是的,这就是他对服务类型暗示所做的。它将服务注入控制器的构造函数中,以便可以使用它。