Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>One would think the access controller would do this for us:</p> <pre><code>access_control: - { role: ROLE_USER, requires_channel: https } - { role: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: http } </code></pre> <p>But, no... I think this would be a very nice feature though.</p> <p>In that case, we can hack something together with a request listener using kernel events:</p> <pre><code>namespace YourBundle\EventListener; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpFoundation\RedirectResponse; class RequestListener { public function onKernelRequest(GetResponseEvent $event) { $request = $event-&gt;getRequest(); // force ssl based on authentication if ($this-&gt;container-&gt;get('security.context')-&gt;isGranted('IS_AUTHENTICATED_FULLY')) { if (!$request-&gt;isSecure()) { $request-&gt;server-&gt;set('HTTPS', true); $request-&gt;server-&gt;set('SERVER_PORT', 443); $event-&gt;setResponse(new RedirectResponse($request-&gt;getUri())); } } else { if ($request-&gt;isSecure()) { $request-&gt;server-&gt;set('HTTPS', false); $request-&gt;server-&gt;set('SERVER_PORT', 80); $event-&gt;setResponse(new RedirectResponse($request-&gt;getUri())); } } } } </code></pre> <p>Define your listener in config.yml under services:</p> <pre><code>myapp.request.listener: class: MyApp\MyBundle\EventListener\RequestListener tags: - { name: kernel.event_listener, event: kernel.request } </code></pre> <p>See <a href="http://symfony.com/doc/current/book/internals.html#internal-requests" rel="nofollow">Symfony Internals</a> for details on events and the like.</p>
 

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