如何从 UserProvider 类访问 EntityManager
我想从 UserProviderClass 访问 EntityManager,以便能够在用户通过 API 登录后立即将用户保留在我的数据库中。
但是这个类实现了UserProviderInterface并且不扩展Controller,所以这个代码无法工作:
$em = $this->getDoctrine()->getEntityManager();
这是完整的类
<?php
namespace am\ContributionBundle\Objects;
use am\ContributionBundle\Entity\User;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
class UserProvider implements UserProviderInterface
{
public function loadUserByUsername($username){
// Processing to the authentification using curl
$url = 'exemple.com/api.php?pseudo='.$username;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$output = json_decode(curl_exec($ch));
curl_close($ch);
try {
$user = // ... looking for user in DB
// How to ??
if (null === $user) {
// ... user creation
}
return $user;
} catch (\Exception $e) {
echo $e->getMessage();
throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
}
}
function supportsClass($class){}
function refreshUser(UserInterface $user){
return $this->loadUserByUsername($user->getUsername());
}
}
如何在这里获取EntityManager的实例?
I would like to acces to the EntityManager from a UserProviderClass for be able to instantly persist users in my database after they log-in via an API.
But this class implements UserProviderInterface and do not extend Controller so this code can't work:
$em = $this->getDoctrine()->getEntityManager();
Here is the full class
<?php
namespace am\ContributionBundle\Objects;
use am\ContributionBundle\Entity\User;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
class UserProvider implements UserProviderInterface
{
public function loadUserByUsername($username){
// Processing to the authentification using curl
$url = 'exemple.com/api.php?pseudo='.$username;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$output = json_decode(curl_exec($ch));
curl_close($ch);
try {
$user = // ... looking for user in DB
// How to ??
if (null === $user) {
// ... user creation
}
return $user;
} catch (\Exception $e) {
echo $e->getMessage();
throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
}
}
function supportsClass($class){}
function refreshUser(UserInterface $user){
return $this->loadUserByUsername($user->getUsername());
}
}
How can I get an instance of the EntityManager here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试扩展 Symfony\Component\DependencyInjection\ContainerAware。然后你就可以使用容器来查找任何对象。
另请记住将其配置为服务: http://symfony.com/doc/current/书/service_container.html
Try extending Symfony\Component\DependencyInjection\ContainerAware. Then you can use the container to find any object.
Also remember to configure it as a service: http://symfony.com/doc/current/book/service_container.html