I am using symfony2 and FOSUserBundle.
Normally,I can get user data from Controller
$user = $this->get('security.context')->getToken()->getUser();
or
$user = $this->container->get('security.context')->getToken()->getUser();
But now,I want to get the userdata from eventlistner which are called from Ajax.
Is it possible? or not?
my source is here.
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);
}
}
}
according to @nifr's advice.
I changed a bit in my source cord like
class CalendarEventListener
{
private $entityManager;
private $container;//add
public function __construct(
EntityManager $entityManager,
ContainerInterface $container)//add
{
$this->entityManager = $entityManager;
$this->container = $container;//add
}
then I want to pass the second argument to eventlistner. I am using services.xml to make listener. How can I add second argument on this?
<?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>
Be sure to not have the
Symfony profiler/toolbar
turned on. I don't know why, but that made$this->container->get('security.context')->getToken()
returnnull
every time.In the file,
config_dev.yml
make sure the following is set:You need to inject the container via Dependency Injection into your listener class in order to acccess it.
Read more about it in the book chapter.
Though injecting the whole container is performance-wise not the best idea among with some other reasons like testability ( you should normally only inject the services you need in your class instead of the container )...
...in your case if you aren't using a UserCallable you will get a circular reference when injecting
@security.context
.So the quickest solution is injecting the container and adjusting your listener's constructor:
As of Symfony 2.6 this issue should be fixed. A pull request has just been accepted into the master. Your problem is described in here. https://github.com/symfony/symfony/pull/11690
As of Symfony 2.6, you can inject the
security.token_storage
into your listener. This service will contain the token as used by theSecurityContext
in <=2.5. In 3.0 this service will replace theSecurityContext::getToken()
altogether. You can see a basic change list here: http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements#deprecated-the-security-context-serviceExample usage in 2.6:
Your configuration:
Your Listener