【问题标题】:Symfony - redirect 404 to a custom template only from one URLSymfony - 仅从一个 URL 将 404 重定向到自定义模板
【发布时间】:2019-03-15 23:57:02
【问题描述】:

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

此时我不想要任何出色的解决方案,我只想在房间不再可用时将用户移动到自定义树枝模板 - 比方说。 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(),
    ]);
}

【问题讨论】:

  • 请不要破坏您的帖子。这可能会导致question ban。通过在 Stack Exchange 网络上发布,您已授予 SE 分发该内容的不可撤销的权利(在 CC BY-SA 3.0 license 下)。根据 SE 政策,任何破坏行为都将被撤销。如果您想取消此帖子与您的帐户的关联,请参阅What is the proper route for a disassociation request?
  • 假设是多次点击失误。 ;)
  • 在在线工作聊天中,我确实有一只猫在我的键盘上跑来跑去。它确实发生了

标签: php symfony


【解决方案1】:

如果您使用的是 Symfony 4,那么您需要在 templates/bundles/TwigBundle/Exception/ 中创建模板 error404.html.twig,因此 404 页面将呈现来自该 error404.html.twig(自定义模板)的内容。

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

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

【讨论】:

  • 不幸的是,这有点不同,因为它是 Twig 根据它从 API 接收到的内容做出异常,它不是典型的 404。在这种情况下,我想在我的代码中关闭它已共享并转发到自定义的 404x.html.twig 模板或将所有异常从 chat/video_join.html.twig(假设)转发到自定义的 404x.html.twig
猜你喜欢
  • 2013-06-12
  • 2015-08-01
  • 1970-01-01
  • 2011-11-30
  • 2015-03-11
  • 2012-03-12
  • 2015-09-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多