Symfony - 仅从一个 URL 将 404 重定向到自定义模板 [英] Symfony - redirect 404 to a custom template only from one URL

查看:18
本文介绍了Symfony - 仅从一个 URL 将 404 重定向到自定义模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户想要开始彼此之间的视频聊天时,我在我的页面上创建了视频室(通过 Twig).当他们离开房间时,房间会在一段时间后被删除,他们必须创建一个新房间才能再次开始交谈.问题是我有一个通知系统,它会在您的通知中留下旧的 URL,当用户在一段时间后再次尝试点击时,他会收到 404.

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.

此时我不想要任何出色的解决方案,如果房间不再可用,我只想将用户移动到自定义树枝模板 - 比方说.404video.html

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

感谢您的帮助!

/**
 * @Route("/video/join/{room_name}", name="videochat_join")
 *
 * @param $room_name
 *
 * @return RedirectResponse|Response
 *
 * @throws \Twilio\Exceptions\ConfigurationException
 * @throws \Twilio\Exceptions\TwilioException
 */
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(),
    ]);
}

推荐答案

如果你使用的是 Symfony 4 那么你需要在 templates/bundles/TwigBundle/Exception/,所以 404 页面会从这个 error404.html.twig(自定义模板)渲染内容.

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).

您可以在 symfony 官方网站上找到更多详细信息:https://symfony.com/doc/current/controller/error_pages.html

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

请注意,自定义错误页面仅在生产模式下可用,因此如果您想检查自定义 404,那么您必须在生产模式下运行您的应用程序.

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.

这篇关于Symfony - 仅从一个 URL 将 404 重定向到自定义模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆