Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

php - Symfony - redirect 404 to a custom template only from one URL

I have video rooms (via Twig) being created on my page when the users want to start a video chat between each other. When they leave the room, the room is being deleted after a period of time and they have to create a new room in order to start talking again. The problem is that I have a notification system that leaves the old URL in your notifications and when user tries to click that again after some period of time he receives a 404.

I don't want any superb solution at this point, I just want to move the user to custom twig template if the room is no longer available - let's say. 404video.html

Thanks for any help!

/**
 * @Route("/video/join/{room_name}", name="videochat_join")
 *
 * @param $room_name
 *
 * @return RedirectResponse|Response
 *
 * @throws TwilioExceptionsConfigurationException
 * @throws TwilioExceptionsTwilioException
 */
public function joinVideo($room_name)
{
    $user = $this->getCurrentUser();
    $twilio = new Client(getenv('TWILIO_API_KEY'), getenv('TWILIO_API_SECRET'));
    $room = $twilio->video->v1->rooms($room_name)->fetch();
    $roomSid = $room->sid;
    $token = new AccessToken(getenv('TWILIO_ACCOUNT_SID'), getenv('TWILIO_API_KEY'), getenv('TWILIO_API_SECRET'), 3600, $user->getEmail());
    $videoGrant = new VideoGrant();
    $videoGrant->setRoom($room_name);
    $token->addGrant($videoGrant);
    return $this->render('chat/video_join.html.twig', [
        'roomSid' => $roomSid,
        'roomName' => $room_name,
        'accessToken' => $token->toJWT(),
    ]);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you are using the Symfony 4 then you need to create template error404.html.twig in templates/bundles/TwigBundle/Exception/, So 404 page will render content from this error404.html.twig (custom template).

You can find more details on symfony official site: https://symfony.com/doc/current/controller/error_pages.html

Please note that the custom error page available only in production mode, so if you want to check custom 404 then you must have to run your application in production mode.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...