您已请求不存在的服务"security.context" [英] You have requested a non-existent service "security.context"

查看:110
本文介绍了您已请求不存在的服务"security.context"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了服务,但不起作用

i create service but it doesn't work

services:
    redirectionListener:
          class: Front\EcommerceBundle\Listener\RedirectionListener
          arguments: ["@service_container","@session"]
          tags:
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }

这是我的课

and this my class

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')));
            }
        }
    }
}

Container.php第268行中的

ServiceNotFoundException:您已请求 不存在的服务"security.context".

ServiceNotFoundException in Container.php line 268: You have requested a non-existent service "security.context".

推荐答案

security.context服务在2.6中已弃用,并分为两个新服务:security.authorization_checkersecurity.token_storage.

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')) { ... }

公告中的更多信息

希望获得帮助

这篇关于您已请求不存在的服务"security.context"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆