有人可以帮我从 event.controller 获取事件侦听器中的用户 ID 吗?
# EventListener
kernel.listener.corporation.manage:
class: Site\CorporationBundle\Event\SiteCorporationManageListener
arguments: ["@doctrine.orm.entity_manager", "@user.own.item", "@security.context"]
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onKernelRequest }
和监听类
use Doctrine\ORM\EntityManager;
use Site\MainBundle\Service\UserOwnItem;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Core\SecurityContext;
class SiteCorporationManageListener
{
private $oEntityManager = null;
private $oUserOwnItem = null;
private $oSecurityContext = null;
public function __construct(EntityManager $oEntityManager, UserOwnItem $oUserOwnItem, SecurityContext $oSecurityContext)
{
$this->oEntityManager = $oEntityManager;
$this->oUserOwnItem = $oUserOwnItem;
$this->oSecurityContext = $oSecurityContext;
}
public function onKernelRequest(FilterControllerEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
$route = $event->getRequest()->get('_route');
$corporationID = $event->getRequest()->get('corporationID', null);
$userID = $this->oSecurityContext->getToken()->getUser()->getID();
//$userID = 3;
//var_dump($userID);
if (strstr($route, 'corporation')) {
if (!strstr($route, 'corporation_index')) {
$bUserOwn = $this->oUserOwnItem->setUserID($userID)->setItemType('corporation')->setItemID($corporationID)->userOwn();
//var_dump($bUserOwn);
}
}
}
}
}
我稍后会清理它,我尝试不同的方法来做到这一点,但即使通过容器和 security_context 我也无法获取用户 ID。它在 getToken() 方法 = 之后中断。
在这个例子中 $userID 是空的......即使在 getToken()->getUser() 给我空......
FatalErrorException: Error: Call to a member function getUser() on a non-object in /home/dev/public_html/git.eve-ceo/src/Site/CorporationBundle/Event/SiteCorporationManageListener.php line 32
请帮忙。