我正在使用 symfony2 和 FOSUserBundle。
通常,我可以从 Controller 获取用户数据
$user = $this->get('security.context')->getToken()->getUser();
或者
$user = $this->container->get('security.context')->getToken()->getUser();
但是现在,我想从 Ajax 调用的 eventlistner 获取用户数据。
是否可以?或不?
我的来源在这里。
namespace Acme\MemberBundle\EventListener;
use ADesigns\CalendarBundle\Event\CalendarEvent;
use ADesigns\CalendarBundle\Entity\EventEntity;
use Doctrine\ORM\EntityManager;
class CalendarEventListener
{
private $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function loadEvents(CalendarEvent $calendarEvent)
{
$startDate = $calendarEvent->getStartDatetime();
$endDate = $calendarEvent->getEndDatetime();
$user = $this->container->get('security.context')->getToken()->getUser();//it doesnt work
// load events using your custom logic here,
// for instance, retrieving events from a repository
$companyEvents = $this->entityManager->getRepository('UserBundle:MutorSche')
->createQueryBuilder('company_events')
->where('company_events.event_datetime BETWEEN :startDate and :endDate')
->setParameter('startDate', $startDate->format('Y-m-d H:i:s'))
->setParameter('endDate', $endDate->format('Y-m-d H:i:s'))
->getQuery()->getResults();
foreach($companyEvents as $companyEvent) {
// create an event with a start/end time, or an all day event
if ($companyEvent->getAllDayEvent() === false) {
$eventEntity = new EventEntity($companyEvent->getTitle(), $companyEvent->getStartDatetime(), $companyEvent->getEndDatetime());
} else {
$eventEntity = new EventEntity($companyEvent->getTitle(), $companyEvent->getStartDatetime(), null, true);
}
//optional calendar event settings
$eventEntity->setAllDay(true); // default is false, set to true if this is an all day event
$eventEntity->setBgColor('#FF0000'); //set the background color of the event's label
$eventEntity->setFgColor('#FFFFFF'); //set the foreground color of the event's label
$eventEntity->setUrl('http://www.google.com'); // url to send user to when event label is clicked
$eventEntity->setCssClass('my-custom-class'); // a custom class you may want to apply to event labels
//finally, add the event to the CalendarEvent for displaying on the calendar
$calendarEvent->addEvent($eventEntity);
}
}
}
根据@nifr 的建议。
我在源代码中做了一些改变,比如
class CalendarEventListener
{
private $entityManager;
private $container;//add
public function __construct(
EntityManager $entityManager,
ContainerInterface $container)//add
{
$this->entityManager = $entityManager;
$this->container = $container;//add
}
然后我想将第二个参数传递给 eventlistner。我正在使用 services.xml 来制作监听器。我该如何添加第二个论点?
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services">
<services>
<service id="acme.memberbundle.calendar_listener" class="Acme\MemberBundle\EventListener\CalendarEventListener">
<argument type="service" id="doctrine.orm.entity_manager" />
<tag name="kernel.event_listener" event="calendar.load_events" method="loadEvents" />
</service>
</services>
</container>