Laravel 从路由生成安全的 https URL

问题描述:

编辑

我真的找不到从路由名称生成安全 URL 的方法.

I can't really find a way to generate a secure URL from route name.

要获取完整的 URL,我使用

To get a full URL, I use

echo route('my_route_name');

但是,如果我想要一个带有 https 的 URL,该怎么办?

But what to do, if I want a URL with https?

事实证明,laravel 并不关心 url 是否安全,因为它是根据当前 url 生成的.如果您在 https 页面上,route() 将返回安全 url.如果在 http,则 http:// url

Actually turns out, that laravel doesn't care if url is secure or not, because it generates based on the current url. If you're on https page, route() will return secure url. If on http, then http:// url

问题是,Laravel 没有检测到 https 已启用,这是由于服务器配置错误造成的.

The problem was, that Laravel didn't detect that https was enabled, which was due to faulty server configuration.

您可以通过调用 Request::isSecure();