You have requested a non-existent service "security.context"
Asked Answered
M

2

26

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

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

Micheal answered 10/4, 2016 at 15:44 Comment(1)
Couple of unrelated suggestions: No need to inject the entire container when all you need is the router and security.token_storage. And don't inject the session at all, just pull it from the request: $event=>getRequest()->getSession();Sydney
D
55

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

Diez answered 10/4, 2016 at 17:38 Comment(0)
L
4

Following example will work:

/*Symfony 4.1.3*/

$user = $this->get('security.token_storage')->getToken()->getUser();

echo $user->getId(); // USER ID SESSION
Linguini answered 23/8, 2018 at 16:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.