laravel asset()方法不返回https

5 php laravel

我正在使用asset()公共方法在laravel中生成正确的网址。

在文档中说:

在此处输入图片说明

因此,理论上它应该检测正确的方案本身。

但是在代码中我看到了:https : //github.com/illuminate/routing/blob/master/UrlGenerator.php#L210

public function asset($path, $secure = null)
    {
Run Code Online (Sandbox Code Playgroud)

安全的默认值为null。因此,此方法对http / https都不适用。

我在这里想念什么?

我正在使用反向代理,可能是因为这个吗?

Fra*_*ula 3

正如您从 GitHub 中看到的,该asset方法正在调用getScheme以确定方案应该是什么。

https://github.com/Illuminate/routing/blob/master/UrlGenerator.php#L303

public function formatScheme($secure = null)
{
    if (! is_null($secure)) {
        return $secure ? 'https://' : 'http://';
    }
    if (is_null($this->cachedScheme)) {
        $this->cachedScheme = $this->forceScheme ?: $this->request->getScheme().'://';
    }
    return $this->cachedScheme;
}
Run Code Online (Sandbox Code Playgroud)

因此,如果您不提供asset第二个参数$secure,那么它将使用请求方案。否则,您可以提供$secure强制所需的方案,无论请求中的方案是什么。

如果您查看代码,您会发现 if$secure为 null 并且未设置缓存,则缓存被设置为请求方案(即$this->request->getScheme())并因此返回。

  • 哦,那个死地方:)没有人回答任何有关服务器故障的问题。无论如何,我在 nginx conf 中发现了问题 `proxy_set_header X-Forwarded-Proto https` (3认同)