Q
Q
Quber2014-07-03 06:13:35
symfony
Quber, 2014-07-03 06:13:35

How to pass a service to a Symfony 2 EventListener form listener?

Hello. There is an easy question.
How to pass "router" service to EventListener form listener in Symfony 2?
Below is what I did:

// 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>

After attaching the listener to the form:
Страница не грузится
Код ошибки: ERR_EMPTY_RESPONSE

If you do not connect the listener, then the form works well. If you change the name of the service to a non-existent one in the service.yml file, then there will be an error that such a service does not exist. So the service is being transferred. But where is the mistake?
Thank you.
UPD.
If you specify null in
public function __construct (\Symfony\Bundle\FrameworkBundle\Routing\Router $router = null)
Then the page loads. So nothing arrives in the $router variable. Why?
dev.log file

[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". [] []

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Makarov, 2014-07-03
@onqu

Earlier arguments in yml were without quotes.

// services.yml
services:
    kernel.listener.url:
        class: App\CoreBundle\Form\EventListener\Url
        arguments: [ "@router" ]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question