Symfony 4 - FOSUserBundle - 在自定义路由上呈现用户信息
Symfony 4 - FOSUserBundle - Render user information on custom route
我的自定义 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_name" 和 "first_name()"、"getfirst_name()"/"isfirst_name()"/"hasfirst_name()" 或“__call()”中的任何一种方法都不是在 class "App\Entity\User".
中存在并具有 public 访问权限
如何从数据库中获取用户信息并显示在个人资料的子页面中?
编辑:用户实体...
<?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 层上做的事情非常简单:
check if user is an array and first_name a valid element;
if not, and if user is an object, check that first_name is a valid property;
if not, and if user is an object, check that first_name is a valid method (even if first_name is the constructor - use __construct() instead);
if not, and if user is an object, check that getfirst_name is a valid method;
if not, and if user is an object, check that isfirst_name is a valid method;
if not, and if user is an object, check that hasfirst_name is a valid method;
if not, return a null value.
顺便说一下,您应该遵循变量的 Symfony Coding Standard,因为 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,
));
}
}
@Franck Games 是对的,但您也可以去掉 get
。
如果你写 {{ user.firstName }}
,twig 会自动将其关联到你的方法 getFirstName()
。
我不知道你为什么用 snake_case 编写你的属性,但你可以将其更改为驼峰式并通过它们的 "real" 名称访问你的属性。
我的自定义 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_name" 和 "first_name()"、"getfirst_name()"/"isfirst_name()"/"hasfirst_name()" 或“__call()”中的任何一种方法都不是在 class "App\Entity\User".
中存在并具有 public 访问权限如何从数据库中获取用户信息并显示在个人资料的子页面中?
编辑:用户实体...
<?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 层上做的事情非常简单:
check if user is an array and first_name a valid element; if not, and if user is an object, check that first_name is a valid property; if not, and if user is an object, check that first_name is a valid method (even if first_name is the constructor - use __construct() instead); if not, and if user is an object, check that getfirst_name is a valid method; if not, and if user is an object, check that isfirst_name is a valid method; if not, and if user is an object, check that hasfirst_name is a valid method; if not, return a null value.
顺便说一下,您应该遵循变量的 Symfony Coding Standard,因为 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,
));
}
}
@Franck Games 是对的,但您也可以去掉 get
。
如果你写 {{ user.firstName }}
,twig 会自动将其关联到你的方法 getFirstName()
。
我不知道你为什么用 snake_case 编写你的属性,但你可以将其更改为驼峰式并通过它们的 "real" 名称访问你的属性。