Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You want to create a listener that listens to the <code>kernel.request</code> event (<a href="http://symfony.com/doc/current/cookbook/service_container/event_listener.html" rel="nofollow">documentation here</a>). In that listener you have access to the request, and the container so you can do anything you like. During <code>kernel.request</code> Symfony gives you a <code>GetResponseEvent</code>. </p> <p>You can set a <code>Response</code> object on this event just as you would return a response in a controller. If you do set a response, Symfony will return it and not go through the normal request --> controller --> response cycle.</p> <pre><code>namespace Acme\UserBundle\EventListener; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\HttpKernel; use Symfony\Component\DependencyInjection\ContainerAware; class UserRedirectListener extends ContainerAware { public function onKernelRequest(GetResponseEvent $event) { if (HttpKernel::MASTER_REQUEST != $event-&gt;getRequestType()) { // don't do anything if it's not the master request return; } $user = $this-&gt;container-&gt;get('security.context')-&gt;getToken()-&gt;getUser(); // for example... if ($user-&gt;websiteDisabled === false) { return; } // here you could render a template, or create a RedirectResponse // or whatever it is $response = new Response(); // as soon as you call GetResponseEvent#setResponse // symfony will stop propogation and return the response // no other framework code will be executed $event-&gt;setResponse($response); } } </code></pre> <p>You will also need to register the event listener in one of your config files, for example:</p> <pre><code># app/config/config.yml services: kernel.listener.your_listener_name: class: Acme\UserBundle\EventListener\UserRedirectListener tags: - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload