简体   繁体   English

在Laravel中使用身体返回重定向

[英]Return Redirect with body in Laravel

I'm trying to do a redirect with body in Laravel. 我正在尝试使用Laravel中的body进行重定向。 I've tried this: 我已经试过了:

return Redirect::to($log_in_url)->with('body','<html><head><title>Redirecting to your page</title></head><body>If you are not redirected within 5 seconds, please <a href="'.$log_in_url.'">cl ick here</a>.</body></html>');

I look in the network tab, I don't really see anything. 我在“网络”标签中查看,但没有看到任何内容。

The question is that how would one make a delayed redirection by showing an HTML waiting page before the actual redirection happens? 问题是,在实际重定向发生之前,如何通过显示HTML等待页面来进行延迟重定向?

You're making a handful of false assumptions: 您做出了一些错误的假设:

  • The with method puts the thing into the session so that you can access it after the redirection. with方法将事物放入会话中,以便您在重定向可以访问它。 A common usecase is to set messages and then redirect the user. 一个常见的用例是设置消息,然后重定向用户。
  • Don't expect magic by just setting the thing body . 不要仅仅通过设置物体body期待魔术。
  • There's no such a standardized redirection called "redirect with body" as you stated. 如您所说,没有这样的标准化重定向称为“通过主体重定向” If you need such a thing, you have to implement it. 如果您需要这样的东西,则必须实施它。

I assume you're having one of those vBulletin-like redirect styles in mind. 我假设您正在考虑使用类似vBulletin的重定向样式之一。 To implement it in Laravel context, you gonna need a mediatory view to do a clientside redirect for you after a set amount of delay. 为了在Laravel上下文中实现它,您将需要一个中介视图来在设置一定的延迟后为您执行客户端重定向。 Let's name it redirect.blade.php : 让我们将其命名为redirect.blade.php

<!doctype html>
<html lang="en">
<head>
    <title>Redirecting...</title>
    <script>
        window.setTimeout(function () {
            window.location = "{{ $url }}";
        }, 5000);
    </script>
</head>
<body>
    If you are not redirected within 5 seconds, 
    please <a href="{{ $url }}">click here</a>.
</body>
</html>

With this in place, your controller will pass a $url to this mediatory view and let it be rendered to do the clientside redirection: 完成此操作后,您的控制器会将$url传递到此中介视图,并使其呈现为进行客户端重定向:

# Controller method
return view('redirect', ['url' => $log_in_url])

This style of redirection won't be working if JavaScript is disabled and that's why they put a link into the page content and warn the user about it. 如果禁用了JavaScript,则这种重定向方式将无法正常工作,这就是为什么他们将链接放入页面内容并向用户发出警告的原因。

Some take a hybrid approach: 有些采用混合方法:

<noscript>
    <meta http-equiv="refresh" content="5;url={{ $url }}" />
</noscript>

The reason that they don't go with the refresh header/meta tag in the first place is that it's not specified in the HTTP standard. 他们之所以没有使用refresh header / meta标签,是因为HTTP标准中未指定它。 Read more . 阅读更多


I strongly suggest that you look into alternatives. 我强烈建议您研究替代方案。 This is so 1990 and not user-friendly at all. 情况是如此,1990年,并且根本不友好。

As a visitor, if I deal with a website that makes me wait for 5 godddamn seconds, I'd just leave. 作为访问者,如果我处理的网站让我等待5天该死,那我就走了。 That's why people used to make browser extensions to workaround the vBulletin's login screen waiting time! 这就是为什么人们过去使用浏览器扩展来解决vBulletin的登录屏幕等待时间的原因!

Embrace simplicity and just do a regular HTTP redirect. 拥抱简单性,然后执行常规的HTTP重定向。 It's best for all humanity. 这对全人类都是最好的。

It's not a task for Laravel. 这不是Laravel的任务。 You can just return page with meta, or using javascript. 您可以只返回带有meta或使用javascript的页面。

// Controller
return view('redirect'); 

// View redirect.blade (Regular html page with additional meta tag)
<meta http-equiv="refresh" content="5;url=http://www.google.com/" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM