Quber
@Quber
PHP Team lead

Как передать сервис в слушатель формы EventListener Symfony 2?

Всем привет. Есть лёгкий вопрос.
Как передать сервис "router" в слушатель формы EventListener в Symfony 2?

Ниже приведено, то что сделал я:
// services.yml
services:
    kernel.listener.url:
        class: App\CoreBundle\Form\EventListener\Url
        arguments:
            router: "@router"

<br>
// url.php<br>
namespace App\CoreBundle\Form\EventListener\EventListener;<br><br>
use Symfony\Component\Form\FormEvent;<br>
use Symfony\Component\Form\FormEvents;<br>
use Symfony\Component\EventDispatcher\EventSubscriberInterface;<br><br>
class Url implements EventSubscriberInterface<br>
{<br><br>
    private $router;<br><br>
    /**<br>
     * @param \Symfony\Bundle\FrameworkBundle\Routing\Router $router<br>
     */<br>
    public function __construct (\Symfony\Bundle\FrameworkBundle\Routing\Router $router)<br>
    {<br>
        $this->router = $router;<br>
    }<br><br>
    public static function getSubscribedEvents()<br>
    {<br>
         // some<br>
    }<br><br>
}<br>

После подключения слушателя к форме:
Страница не грузится
Код ошибки: ERR_EMPTY_RESPONSE

Если не подключать слушатель, то форма работает хорошо. Если изменить имя сервиса на несуществующее в файле service.yml то будет ошибка, что такого сервиса не сущесвует. Значит сервис передаётся. Но где ошибка?

Спасибо.

UPD.
Если указать null в
public function __construct (\Symfony\Bundle\FrameworkBundle\Routing\Router $router = null)
Тогда страница грузится. Значит в переменную $router ничего не прилетает. Почему?

файл dev.log

[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest". [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest". [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\FragmentListener::onKernelRequest". [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest". [] []
[2014-07-03 09:52:12] request.INFO: Matched route "admin_menu_create" (parameters: "_controller": "App\CoreBundle\Controller\MenuController::createAction", "_locale": "ru", "_route": "admin_menu_create") [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest". [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.request" to listener "FOS\RestBundle\EventListener\BodyListener::onKernelRequest". [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest". [] []
[2014-07-03 09:52:12] security.DEBUG: Read SecurityContext from the session [] []
[2014-07-03 09:52:12] security.DEBUG: Reloading user from user provider. [] []
[2014-07-03 09:52:12] doctrine.DEBUG: SELECT t0.username AS username1, t0.username_canonical AS username_canonical2, t0.email AS email3, t0.email_canonical AS email_canonical4, t0.enabled AS enabled5, t0.salt AS salt6, t0.password AS password7, t0.last_login AS last_login8, t0.locked AS locked9, t0.expired AS expired10, t0.expires_at AS expires_at11, t0.confirmation_token AS confirmation_token12, t0.password_requested_at AS password_requested_at13, t0.roles AS roles14, t0.credentials_expired AS credentials_expired15, t0.credentials_expire_at AS credentials_expire_at16, t0.id AS id17, t0.image AS image18, t0.register AS register19, t0.name AS name20, t0.sername AS sername21, t0.lang AS lang22 FROM Users t0 WHERE t0.id = ? LIMIT 1 [229] []
[2014-07-03 09:52:12] security.DEBUG: Username "root" was reloaded from user provider. [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest". [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.request" to listener "Knp\Bundle\MenuBundle\EventListener\VoterInitializerListener::onKernelRequest". [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.request" to listener "Knp\Bundle\PaginatorBundle\Subscriber\SlidingPaginationSubscriber::onKernelRequest". [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ErrorsLoggerListener::injectLogger". [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ErrorsLoggerListener::injectLogger". [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector::onKernelController". [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.controller" to listener "Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::onKernelController". [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController". [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController". [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController". [] []
[2014-07-03 09:52:12] doctrine.DEBUG: SELECT t0.id AS id1, t0.title AS title2, t0.lft AS lft3, t0.lvl AS lvl4, t0.rgt AS rgt5, t0.root AS root6, t0.url AS url7, t0.image AS image8, t0.locale AS locale9, t0.parent_id AS parent_id10 FROM Menu t0 WHERE t0.id = ? ["75"] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.exception" to listener "Symfony\Component\Security\Http\Firewall\ExceptionListener::onKernelException". [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.exception" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelException". [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.exception" to listener "Symfony\Component\HttpKernel\EventListener\ExceptionListener::onKernelException". [] []
[2014-07-03 09:52:12] request.CRITICAL: Uncaught PHP Exception Symfony\Component\Debug\Exception\DummyException: "" at F:\domains\test.ekker.loc\vendor\symfony\symfony\src\Symfony\Component\Debug\ErrorHandler.php line 155 {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\DummyException:  at F:\\domains\\test.ekker.loc\\vendor\\symfony\\symfony\\src\\Symfony\\Component\\Debug\\ErrorHandler.php:155)"} []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest". [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest". [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\FragmentListener::onKernelRequest". [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest". [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest". [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.request" to listener "FOS\RestBundle\EventListener\BodyListener::onKernelRequest". [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest". [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest". [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.request" to listener "Knp\Bundle\MenuBundle\EventListener\VoterInitializerListener::onKernelRequest". [] []
[2014-07-03 09:52:12] event.DEBUG: Notified event "kernel.request" to listener "Knp\Bundle\PaginatorBundle\Subscriber\SlidingPaginationSubscriber::onKernelRequest". [] []

  • Вопрос задан
  • 3465 просмотров
Пригласить эксперта
Ответы на вопрос 1
onqu
@onqu
weasy
Раньше аргументы в yml были без кавычек.

// services.yml
services:
    kernel.listener.url:
        class: App\CoreBundle\Form\EventListener\Url
        arguments: [ "@router" ]
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы