本文介绍了您请求了一个不存在的服务“security.context".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了服务但它不起作用
服务:重定向监听器:类:FrontEcommerceBundleListenerRedirectionListener参数:["@service_container","@session"]标签:- { 名称:kernel.event_listener,事件:kernel.request,方法:onKernelRequest }
这是我的课
命名空间 FrontEcommerceBundleListener;使用 SymfonyComponentDependencyInjectionContainerBuilder;使用 SymfonyComponentHttpFoundationSessionSession;使用 SymfonyComponentHttpFoundationRedirectResponse;使用 SymfonyComponentHttpKernelEventGetResponseEvent;类重定向监听器{公共函数 __construct(ContainerBuilder $container, Session $session){$this->session = $session;$this->router = $container->get('router');$this->securityContext = $container->get('security.context');}公共函数 onKernelRequest(GetResponseEvent $event){$route = $event->getRequest()->attributes->get('_route');if ($route == 'livraison' || $route == 'validation') {如果 ($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')));}}}}
Container.php 第 268 行中的 ServiceNotFoundException:您已请求一个不存在的服务security.context".
解决方案
security.context
服务在 2.6 中被弃用,并拆分为两个新服务:security.authorization_checker
和 security.token_storage
.
与之前版本框架的一些不同用法:
//Symfony 2.5$user = $this->get('security.context')->getToken()->getUser();//Symfony 2.6$user = $this->get('security.token_storage')->getToken()->getUser();//Symfony 2.5if (false === $this->get('security.context')->isGranted('ROLE_ADMIN')) { ... }//Symfony 2.6if (false === $this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { ... }
更多信息在本公告
希望对您有所帮助
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')));
}
}
}
}
解决方案
The security.context
service was deprecated in the 2.6 and split into two new services: security.authorization_checker
and security.token_storage
.
Some different usage from the prior version of the framework:
// Symfony 2.5
$user = $this->get('security.context')->getToken()->getUser();
// Symfony 2.6
$user = $this->get('security.token_storage')->getToken()->getUser();
// Symfony 2.5
if (false === $this->get('security.context')->isGranted('ROLE_ADMIN')) { ... }
// Symfony 2.6
if (false === $this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { ... }
More info in this announcement
Hope this help
这篇关于您请求了一个不存在的服务“security.context".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!