i create service but it doesn't work
services:
redirectionListener:
class: FrontEcommerceBundleListenerRedirectionListener
arguments: ["@service_container","@session"]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
and this my class
namespace FrontEcommerceBundleListener;
use SymfonyComponentDependencyInjectionContainerBuilder;
use SymfonyComponentHttpFoundationSessionSession;
use SymfonyComponentHttpFoundationRedirectResponse;
use SymfonyComponentHttpKernelEventGetResponseEvent;
class RedirectionListener
{
public function __construct(ContainerBuilder $container, Session $session)
{
$this->session = $session;
$this->router = $container->get('router');
$this->securityContext = $container->get('security.context');
}
public function onKernelRequest(GetResponseEvent $event)
{
$route = $event->getRequest()->attributes->get('_route');
if ($route == 'livraison' || $route == 'validation') {
if ($this->session->has('panier')) {
if (count($this->session->get('panier')) == 0)
$event->setResponse(new RedirectResponse($this->router->generate('panier')));
}
if (!is_object($this->securityContext->getToken()->getUser())) {
$this->session->getFlashBag()->add('notification','Vous devez vous identifier');
$event->setResponse(new RedirectResponse($this->router->generate('fos_user_security_login')));
}
}
}
}
ServiceNotFoundException in Container.php line 268: You have requested
a non-existent service "security.context".
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…