【问题标题】:Laravel form doesn't return https urlLaravel 表单不返回 https url
【发布时间】:2022-01-09 14:27:55
【问题描述】:

我正在使用Laravel Formhttps,但我在使用Form 组件时遇到了困难。出于某种原因,Laravel 总是为 Form 组件生成的所有 url 返回 http:// 协议。

这是我的.env

APP_NAME=Laravel
APP_ENV=local
APP_DEBUG=true
APP_URL=https://example.com

header.blade.php

{!! Form::open(['route' => 'boilerplate.logout', 'method' => 'post', 'id' => 'logout-form']) !!}
<button type="submit" class="btn nav-link d-flex align-items-center logout px-2" data-question="{{ __('boilerplate::layout.logoutconfirm') }}">
    <span class="fa fa-fw fa-power-off hidden-xs pr-1"></span>
</button>
{!! Form::close() !!}

routes/boilerplate.php

Route::post('logout', [LoginController::class, 'logout'])->name('logout');

表单应该返回:

<form method="POST" action="https://example.com/admin/logout" accept-charset="UTF-8" id="logout-form">

但我明白了:

<form method="POST" action="https://example.com/admin/logout" accept-charset="UTF-8" id="logout-form">

我发现了一个类似的问题,一位用户提议添加:

\URL::forceScheme('https');

AppServiceProvider.php 内但是,我想知道为什么会出现以下行为。这是框架的问题还是我做错了什么?

【问题讨论】:

  • 你在代理后面吗?你访问的地址是https
  • @lagbox 是的,我正在使用代理,我能够修复这个更新 nginx 配置的问题,特别是我添加了这个:fastcgi_param HTTPS "on";

标签: php laravel laravel-8


【解决方案1】:

因为 laravel 不关心 schemeAPP_URL,所以 laravel 检查服务器是否支持 https,如果你在生产服务器中并且 https 已经实现,它将自动使用https协议

Laravel 在symfony http foundation 中的URLGenerator.php 中进行大量中继,您可以查看$forceScheme 是否为null,它将回退到$this-&gt;request-&gt;getScheme(),这是(symfony http 基金会)Request.php 的实例

如果您查看symfony http foundationRequest.php,您可以看到方法(isSecure and getScheme)

public function isSecure()
{
    if ($this->isFromTrustedProxy() && $proto = $this->getTrustedValues(self::HEADER_X_FORWARDED_PROTO)) {
        return \in_array(strtolower($proto[0]), ['https', 'on', 'ssl', '1'], true);
    }

    $https = $this->server->get('HTTPS');

    return !empty($https) && 'off' !== strtolower($https);
}

public function getScheme()
{
    return $this->isSecure() ? 'https' : 'http';
}

如您所见,在isSecure 方法中,它检查request 是否来自trusted proxies,或者$_SERVER['HTTPS'] 是否为on

通过设置URL::forceScheme('https');,它将强制为https,而不是使用$this-&gt;request-&gt;getScheme()

【讨论】:

    猜你喜欢
    • 2014-01-03
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 2014-03-11
    • 2015-04-17
    • 2019-03-11
    • 2017-08-06
    相关资源
    最近更新 更多