1

I am looking for a way to return a twig view, wait 5 seconds and then send the user to an external URL.

When I visit the URL, I only see a white page as the template doesn't get an opportunity to load.

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class RedirectController extends AbstractController
{
    public function index(Request $request)
    {
        // TO DO - check if url is valid
        $url = $request->get('url');

        $this->redirectAway($url);

        return $this->render('base/redirect.html.twig', ['url' => $url]);
    }

    public function redirectAway($url)
    {
        $response = new Response();
        $response->setStatusCode(200);
        $response->headers->set('Refresh', '5; url=https://' . $url);
        $response->send();
        return $response;
    }
}

Any help would be most appreciated! <3 I am trying to implement a redirect notice for users for external URLs.

Lewis
  • 13
  • 2
  • 2
    This is the sort of thing you do at the browser level: https://stackoverflow.com/questions/3292038/redirect-website-after-certain-amount-of-time And since you tagged this with Symfony, I'll also point you to an excellent Symfony article explaining the Request/Response paradigm: https://symfony.com/doc/current/introduction/http_fundamentals.html – Cerad Aug 15 '20 at 14:36
  • Does this answer your question? [Redirect website after certain amount of time](https://stackoverflow.com/questions/3292038/redirect-website-after-certain-amount-of-time) – Nico Haase Aug 16 '20 at 08:57

1 Answers1

0

Your redirectAway method creates a Response object and returns it to the caller.
Your index method calls redirectAway, and ignores its return value. Instead, of calling response->send, you should pass the response object as the 3rd parameter in render:

return $this->render(
    'base/redirect.html.twig', 
    ['url' => $url], 
    $this->redirectAway()
);
Daniel
  • 4,481
  • 14
  • 34
  • Hi, thank you for your response! Your answer was fantastic, thank you so much for helping me out! – Lewis Aug 15 '20 at 15:27