【发布时间】:2021-04-03 03:31:29
【问题描述】:
一个简单但愚蠢的问题是今晚在 symfony 上阻止我...... 我需要使用安全组件的 UserInterface 类来检索有关当前用户的信息。但是 symfony 告诉我这个类不存在。我检查了“安全性”是否已安装好,路径也很好......
我的代码:
<?php
namespace App\Controller;
use App\Entity\Profile;
use App\Entity\Candidature;
use App\Form\CandidatureType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class CandidateController extends AbstractController
{
/**
* @Route("/candidate", name="candidate")
*/
public function new(Request $request, UserInterface $user): Response
{
// NEED TO BE CONNECTED !!
if ($user->getUsername()) {
// SOME CODE ...........
} else {
return $this->redirectToRoute('security_login');
}
}
}
我得到的错误(引用)
无法自动装配参数 $user of “App\Controller\CandidateController::new()”:它引用接口 “Symfony\Component\Security\Core\User\UserInterface”但没有这样的 服务存在。您是否创建了实现此接口的类?
【问题讨论】:
-
如果您碰巧使用的是 Symfony 5.2+,那么有一个名为 CurrentUser 的新 PHP 属性将允许您的代码工作。
标签: class symfony security components loading