2
votes

I'm a newbie in laravel, so maybe please don't beat me :-)

I try to get the referrer inside a controller in this way

$from=Request::server('HTTP_REFERER');

As result, I get this error

Non-static method Illuminate\Http\Request::server() should not be called statically, assuming $this from incompatible context

At the top from the controller is

namespace Common\Auth\Controllers;

use Auth;
use Illuminate\Http\Request;
use Common\Settings\Settings;
use Common\Core\Controller;
use Common\Core\BootstrapData;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Common\Auth\User;

Hope somebody can show me how I can get the referrer a give me a little lesson about that.

Thanks a lot.

Update.

In the meantime i've add "function name(Request $request)" to my function and inside the function "$this->request = $request;"

Now i can access with "$this->request->ip();" some informations.

If i try to echo that "$this->request->header('HTTP_REFERRER')" nothing will display?

7
Keep in mind that the official HTTP_REFERRER header is spelled incorrectly as HTTP_REFERER (notice it doesn't have a double R there). - Magmatic

7 Answers

1
votes

To get the full URL for the previous request, use the Laravel helper function

url()->previous();

You can also access it via the URL facade

URL::previous();

Be sure to resolve the import if you are using the URL facade.

use Illuminate\Support\Facades\URL;

Laravel doc

1
votes

You can get the referer from the headers of the request:

$uri = $request->header('referer');

In which $request is the default injected Illuminate\Http\Request

Read more on the headers of the request

0
votes

I dont know why laravel request facade not working like above, but you can get header like this

function example(Request $request) {
    dd($request->header('HTTP_REFERRER'));
}

make sure your method accessed from route

0
votes

You can also get the referer directly from PHP by using $_SERVER['HTTP_REFERER'] instead.

0
votes
// with request helper function from anywhere in your application
$from = request()->getSchemeAndHttpHost();

// with request method. this return http:// part too
$from = $request->getHttpHost();

source: https://stackoverflow.com/a/39835651/10482374

0
votes

please change this:

use Illuminate\Http\Request;

with:

use Request;
0
votes

Reviewing the URL::previous() handle in UrlGenerator@previous reveal accessing the referrer with $this->request->headers->get('referer');