RequestListener in Symfony2



我正在使用Symfony2.3。我创建requestListener并在用户登录时编写并进行家庭操作然后获得了一些奖励点。这正在工作。但是,只有在用户首次回家行动时,我才能完成这一点。如果用户回家多个一次,然后不更新奖励积分?

requestListner.php

<?php
namespace XXXABCBundleEventListener;
use SymfonyComponentHttpKernelEventGetResponseEvent;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentDependencyInjectionContainerInterface;
use SymfonyComponentSecurityCoreSecurityContextInterface;
use SymfonyComponentHttpKernelHttpKernel;
use DoctrineORMEntityManager;
use ThinkdigitUserBundleEntityUser;

class RequestListener
{
/**
 * @var SymfonyComponentSecurityCoreSecurityContextInterface
 */
protected $securityContext;
protected $em;
/**
 * Container
 *
 * @var ContainerInterface
 */
protected $container;
/**
 * Listener constructor
 *
 * @param ContainerInterface $container
 */
public function __construct(ContainerInterface $container, SecurityContextInterface $securityContext, EntityManager $em)
{
    $this->container = $container;
    $this->securityContext = $securityContext;
    $this->em = $em;
}
/**
 * kernel.request Event
 *
 * @param GetResponseEvent $event
 */
public function onKernelRequest(GetResponseEvent $event)
{
    $request  = $event->getRequest();
    $controller = $request->get('_controller');

$home     = array('XXXABCBundleControllerPageHomeController::indexAction', 'XXXABCBundleControllerRegistrationController::confirmedAction');

  //print_r($controller);die;
    if ( $this->container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY'))
    {
        if(in_array($controller,$home)){
            $user = $this->container->get('security.context')->getToken()->getUser();
            //print_r($user->getRewardPoints());
            $point = '5';
            $rewrdPoint = $user->getRewardPoints() + $point;
            $user->setRewardPoints($rewrdPoint);
            $this->em->persist($user);
            $this->em->flush($user);
            //die;
        }
    }
    // Here you can intercept all HTTP requests, and through $container get access to user information
}
}

这与Symfony无关;最简单的方法是将某些内容存储在会话中的检查;

喜欢,在登录用户的打开主页上,只需设置一个会话var,然后在处理请求侦听器之前检查会话var;

if($controller == $home && $this->container->get('session')->get('reward_was_payed', 0) != 1){
    $user = $this->container->get('security.context')->getToken()->getUser();
    //print_r($user->getRewardPoints());
    $point = '5';
    $rewrdPoint = $user->getRewardPoints() + $point;
    $user->setRewardPoints($rewrdPoint);
    $this->em->persist($user);
    $this->em->flush($user);
    $this->container->set('reward_was_payed', 1);
}
;

相关内容

  • 没有找到相关文章

最新更新