undefined方法appDevDebugProjectContainer Listener Symfony 2.1.8

时间:2013-04-02 22:56:44

标签: symfony listener

我尝试用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()

my 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",
    },

1 个答案:

答案 0 :(得分:0)

Configurazione类中,您有三个参数:安全上下文,服务容器和rounter。在您以错误的顺序输入[@ 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');
}