Location headers in Laravel

2019-03-31 07:34发布

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.

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

Any idea how I can fix this?

5条回答
forever°为你锁心
2楼-- · 2019-03-31 07:40

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.

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.

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.

查看更多
孤傲高冷的网名
3楼-- · 2019-03-31 07:45

For example, For this route:

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

In laravel, you can redirect to url simply using

return Redirect::to('hello');

Alternatively, you can redirect to named route simply using

return Redirect::route('hello_name'); 
查看更多
贪生不怕死
4楼-- · 2019-03-31 07:50
return redirect()->to('url')->send();

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

查看更多
三岁会撩人
5楼-- · 2019-03-31 07:56

exit; after header to stop further code execution.

<?php
header('Location: http://www.example.com/');
exit;
查看更多
Deceive 欺骗
6楼-- · 2019-03-31 08:02

try

return Redirect::to('url');
查看更多
登录 后发表回答