您请求了不存在的服务“security.context”
2022-08-30 16:29:32
我创建服务,但它不起作用
services:
redirectionListener:
class: Front\EcommerceBundle\Listener\RedirectionListener
arguments: ["@service_container","@session"]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
而这个我的班级
namespace Front\EcommerceBundle\Listener;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
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行 268:您请求了一个不存在的服务“security.context”。