有遇到需要在构造函数注入UserInterface来进行用户数据相关操作的需求,然后发现竟然无法注入成功!
报错信息如下:
Cannot autowire service "App\Controller\Api\SubordinateController": argument "$tokenStorage" of method "__construct()" references interface "Symfony\Component\Security\Core\User\UserInterface" but no such service exists. Did you create a class that implements this interface?
so,wtf啊,框架貌似 认为它不属于一个服务,所以不可直接注入,只能使用替代法
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
然后用以下写法即可实现Userinterface的效果
public function __construct(TokenStorageInterface $tokenStorage)
{
$user=$tokenStorage->getToken()->getUser();
dd($user);
}
0 条评论