0

我尝试使用 onKernelRequest 进行自定义侦听器,但出现此错误致命错误:调用未定义方法 appDevDebugProjectContainer::generate()

我的服务是:

services:
kernel.listener.Configurazione:
    class: My\ConfigurazioneBundle\Listener\Configurazione 
    tags:
      - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
    arguments: [@security.context,@router,@service_container]  

我的听众是:

namespace My\ConfigurazioneBundle\Listener;

use My\ConfigurazioneBundle\Event\ConfigurazioneEvent;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class Configurazione {

    protected $sicurezza;
    protected $router;
    protected $container;

    function __construct(SecurityContextInterface $securityContext, $container, $router) {
        $this->sicurezza = $securityContext;
        $this->container = $container;
        $this->router = $router;
    }

    public function onKernelRequest(GetResponseEvent $event) {

        $user = $this->sicurezza->getToken()->getUser();
        $container = $this->container;
        $url = $this->router->generate('homepage');
    }

}

我有这个错误

致命错误:调用未定义的方法 appDevDebugProjectContainer::generate()

我的 composer.json

    "require": {
        "php": ">=5.3.3",
        "symfony/symfony": "2.1.8",
        "doctrine/orm": ">=2.2.3,<2.4-dev",
        "doctrine/doctrine-bundle": "1.1.*",
        "twig/extensions": "1.0.*@dev",
        "symfony/assetic-bundle": "2.1.*",
        "symfony/swiftmailer-bundle": "2.1.*",
        "symfony/monolog-bundle": "2.1.*",
        "sensio/distribution-bundle": "2.1.*",
        "sensio/framework-extra-bundle": "2.1.*",
        "sensio/generator-bundle": "2.1.*",
        "jms/security-extra-bundle": "1.2.*",
        "jms/di-extra-bundle": "1.1.*",
        "kriswallsmith/assetic": "1.1.*@dev",
        "stof/doctrine-extensions-bundle": "dev-master",
        "friendsofsymfony/user-bundle": "*",
        "punkave/symfony2-file-uploader-bundle":"*",
        "willdurand/expose-translation-bundle": "dev-master",
        "apy/jsfv-bundle":"dev-master",
        "cypresslab/compass-elephant-bundle":"dev-master",
    },
4

1 回答 1

0

Configurazione类中,您有三个参数:安全上下文、服务容器和路由器。在服务中,您以错误的顺序输入了 [@security.context,@router,@service_container]。

因此,PHP 尝试执行类generate的方法Container,而不是Router

最好定义参数类型以避免这种错误。在您的情况下,它应该是:

public function __construct(SecurityContextInterface $securityContext, ContainerInterface $container, RouterInterface $router)

您也可以只传递容器并从构造函数中获取相应的服务:

public function __construct(ContainerInterface $container)
{
    $this->router = $container->get('router');
    $this->securityContext = $container->get('security.context');
}
于 2013-04-02T23:01:55.070 回答