Laravel 中的位置标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15643718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:38:59  来源:igfitidea点击:

Location headers in Laravel

phpredirectheaderlocationlaravel

提问by GTF

I'm working with a Library for my university's authentication system (Ucam_Webauth) which means I have to redirect to the authentication server in one of the methods. Unfortunately, I cannot return a Redirect:to()because of the architecture of this library. The library itself uses header('Location: ...');but this isn't working for some reason.

我正在为我大学的身份验证系统 (Ucam_Webauth) 使用图书馆,这意味着我必须使用其中一种方法重定向到身份验证服务器。不幸的是,Redirect:to()由于该库的架构,我无法返回 a 。库本身使用,header('Location: ...');但由于某种原因这不起作用。

If I make the programme die();after sending the header it works, but otherwise it doesn't.

如果我die();在发送标头后制作程序,它就可以工作,否则就不行。

Any idea how I can fix this?

知道如何解决这个问题吗?

回答by Alex

return redirect()->to('url')->send();

Sends HTTP headers and content. In my application, send() method acts like 'exit()' and is testable

发送 HTTP 标头和内容。在我的应用程序中,send() 方法的作用类似于“exit()”并且是可测试的

回答by Collin James

I'm not sure I follow. Laravel sets the Location header as part of the Redirect::to() method. If you want to more explicitly define the response you could do it like this.

我不确定我是否遵循。Laravel 将 Location 标头设置为 Redirect::to() 方法的一部分。如果您想更明确地定义响应,您可以这样做。

return Response::make( '', 302 )->header( 'Location', $url );

If that doesn't work I'd probably just fall back on the php stdlib header() and return null.

如果这不起作用,我可能会回到 php stdlib header() 并返回 null。

If all of this still doesn't do any good, maybe the profiler is messing things up. If it is turned on, try disabling it in the config.

如果所有这些仍然没有任何好处,可能是分析器把事情搞砸了。如果它已打开,请尝试在配置中禁用它。

回答by Geert

exit; after header to stop further code execution.

出口; 在标头之后停止进一步的代码执行。

<?php
header('Location: http://www.example.com/');
exit;

回答by Abhishek Goel

try

尝试

return Redirect::to('url');

回答by vijayrana

For example, For this route:

例如,对于这条路线:

Route::get('hello', array('as' => 'hello_name', 'uses' => 'HelloController@getHello'));

In laravel, you can redirect to url simply using

在 Laravel 中,您可以简单地使用重定向到 url

return Redirect::to('hello');

Alternatively, you can redirect to named route simply using

或者,您可以简单地使用重定向到命名路由

return Redirect::route('hello_name');