欢迎各位兄弟 发布技术文章
这里的技术是共享的
$request->headers->get('referer') //下面这个就是获得所有
$request->headers->all()
PUBLISHED 2 YEARS AGO BY FRANKIEC91
How to get Referer URL in Laravel 5 ?
This might be what you want:
URL::previous();
http://laravel.com/api/5.0/Illuminate/Routing/UrlGenerator.html#method_previous
or if you want to send the user "back" to the previous page in your controller, this would work:
Redirect::back();
Hi amt925,
Thanks for the answer but it does not work with cloud/cluster hosting.
URL::previous
and Redirect::back()
will take you back a page (within your app), not give you the referer.
What you're probably after is Request::server('HTTP_REFERER')
.
Further details in the API docs.
You can use Illuminate\Routing\UrlGenerator
, which is what the default getRedirectUrl()
function does (it's in the ValidatesRequests
trait used by Controller
).
app('Illuminate\Routing\UrlGenerator')->previous()
public function previous()
{
$referrer = $this->request->headers->get('referer');
$url = $referrer ? $this->to($referrer) : $this->getPreviousUrlFromSession();
return $url ?: $this->to('/');
}
UrlGenerator isn't what you want because it does more than just get the referrer.
Request::server('HTTP_REFERER')
would be fine, but the the preferred way seems to be Request::headers->get('referer')
.
You may also already have $request injected into your function, or you can use the helper like this:
request()->headers->get('referer')
来自 https://laracasts.com/discuss/channels/general-discussion/laravel-5-referrer-url
$referer = Request::referer();//这应该是laravel 4 中的代码
// or
// $referer = Request::server('HTTP_REFERER');
if (strpos($referer,'admin') !== false) {
dd('coming from admin')
}
Edit #1: As pointed out by @tomvo you can also use URL::previous()
instead of Request::referer()
in L4
Edit #2: It's actually mispelled as referer
instead of referrer
as point out by @JamesF
Edit #3: In Laravel 5 the Request::referer()
method doesn't seem to exist anymore, you can still get it by using Request::header('referer')
as point out by @TheSerenin
Using Request::header('refer') will only work for POST requests. GET requests are the one your're looking for.
You can use Request::segment(1) or Request::segment(2), depends on the exact URL you're using.