Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The method <a href="http://api.symfony.com/2.3/Symfony/Bundle/FrameworkBundle/Controller/Controller.html#method_redirect" rel="noreferrer"><code>Controller::redirect()</code></a> is in fact, creating a new <a href="http://api.symfony.com/2.3/Symfony/Component/HttpFoundation/RedirectResponse.html" rel="noreferrer"><code>RedirectResponse</code></a> object.<br> The default template is <a href="https://github.com/symfony/symfony/blob/2.3/src/Symfony/Component/HttpFoundation/RedirectResponse.php#L88" rel="noreferrer">hard-coded</a> into the response but here are some workarounds.</p> <blockquote> <p><em>In this example, I will use a TWIG template, hence I need the <code>@templating</code> service, but you can use whatever you want to render the page.</em></p> </blockquote> <p>First, create your template <code>301.html.twig</code> into your <code>Acme/FooBundle/Resources/views/Error/</code> with the content you want.</p> <p><kbd><strong>@AcmeFooBundle/Resources/views/Error/301.html.twig</strong></kbd></p> <pre><code>&lt;!DOCTYPE HTML&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;meta http-equiv="refresh" content="1;url={{ uri }}" /&gt; &lt;/head&gt; &lt;body&gt; You are about to be redirected to {{ uri }} &lt;/body&gt; &lt;/html&gt; </code></pre> <h1>From an event listener</h1> <p>If you want this template to be global on any <code>RedirectResponse</code> you can create an event listener which will listen to the response and check whether the response given is an instance of <code>RedirectResponse</code><br> This means that you can still use <code>return $this-&gt;redirect</code> in your controller, only the content of the response will be affected.</p> <p><kbd><strong>services.yml</strong></kbd></p> <pre><code>services: acme.redirect_listener: class: Acme\FooBundle\Listener\RedirectListener arguments: [ @templating ] tags: - name: kernel.event_listener event: kernel.response method: onKernelResponse </code></pre> <p><kbd><strong>Acme\FooBundle\Listener\RedirectListener</strong></kbd></p> <pre><code>use Symfony\Component\Templating\EngineInterface; use Symfony\Component\HttpKernel\Event\FilterResponseEvent; use Symfony\Component\HttpFoundation\RedirectResponse; class RedirectListener { protected $templating; public function __construct(EngineInterface $templating) { $this-&gt;templating = $templating; } public function onKernelResponse(FilterResponseEvent $event) { $response = $event-&gt;getResponse(); if (!($response instanceof RedirectResponse)) { return; } $uri = $response-&gt;getTargetUrl(); $html = $this-&gt;templating-&gt;render( 'AcmeFooBundle:Error:301.html.twig', array('uri' =&gt; $uri) ); $response-&gt;setContent($html); } } </code></pre> <h1>From a controller</h1> <p>Use this if you want to change the template directly from an action.<br> The modification will only be available for the given action, not global to your application.</p> <pre><code>use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpKernel\Event\FilterResponseEvent; class FooController extends Controller { public function fooAction() { $uri = $this-&gt;generateUrl($_redirectTo); $response = new RedirectResponse($uri, 301); $response-&gt;setContent($this-&gt;render( 'AcmeFooBundle:Error:301.html.twig', array( 'uri' =&gt; $uri ) )); return $response; } } </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