對於Symfony 2.8,此代碼$this->container
爲空。嘗試使用ContainerAwareInterface時容器未設置
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
class EntrarYPreregistroFormSubscriber implements EventSubscriberInterface, ContainerAwareInterface
{
use ContainerAwareTrait;
public function preSetData(FormEvent $event)
{
$l = $this->container->get('logger');
$l->notice('GOT LOGGER');
}
....
}
而且我EntrarYPreregistroFormSubscriber
服務被配置爲:
pmktconcursos.entrarypreregistro.form.subscriber:
class: PMKT\ConcursosBundle\EventListener\EntrarYPreregistroFormSubscriber
calls:
- [ setContainer,[ "@service_container" ] ]
我得到異常的$l = $this->container->get('logger');
request.CRITICAL: Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalErrorException: "Error: Call to a member function get() on a non-object" at /Users/vmarquez/Proyectos/GrupoPMKT/Promoticket/current/dev/chedraui1607/chedraui1607/src/PMKT/ConcursosBundle/Form/EventListener/EntrarYPreregistroFormSubscriber.php line 30 {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalErrorException(code: 0): Error: Call to a member function get() on a non-object at /Users/vmarquez/Proyectos/GrupoPMKT/Promoticket/current/dev/chedraui1607/chedraui1607/src/PMKT/ConcursosBundle/Form/EventListener/EntrarYPreregistroFormSubscriber.php:30)"}
我缺少的東西?
爲什麼你不使用通常的方式來傳遞參數到服務?只需將容器定義爲參數,並將其傳遞給構造函數? –
這是我使用Denis的解決方案。即使如此,我很好奇我在做什麼使用特質的問題。 – vmarquez