在本地和生产中在 http 和 https 之间切换 Laravel 资产

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22828238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:18:03  来源:igfitidea点击:

Switching Laravel assets between http and https in local and production

httpslaravellaravel-4

提问by alexleonard

I'm developing an application that will eventually need to be fully HTTPS but I'm temporarily developing locally on HTTP.

我正在开发一个最终需要完全使用 HTTPS 的应用程序,但我暂时在 HTTP 上进行本地开发。

If I use URL::to_asset('path', false)locally then I'd have to go and change every instance of that to true when I switch to HTTPS.

如果我URL::to_asset('path', false)在本地使用,那么当我切换到 HTTPS 时,我必须将它的每个实例更改为 true。

At the moment I'm thinking a Config::get('app.https', true)as the second argument would be the easiest way around this but I was wondering whether there's a more system-wide approach for ensuring your assets follow the routes (for example if I had an application that had partial usage of HTTPS routes it would be nice if Laravel automatically worked out whether you're on a HTTPS route and returned the correct asset link).

目前,我认为Config::get('app.https', true)第二个参数将是解决此问题的最简单方法,但我想知道是否有更全系统的方法来确保您的资产遵循路线(例如,如果我有一个部分使用的应用程序HTTPS 路由,如果 Laravel 自动计算出您是否在 HTTPS 路由上并返回正确的资产链接,那就太好了)。

Or is it possible to get assets to use the "//domain.tld/path/to/something"approach

或者是否有可能让资产使用该"//domain.tld/path/to/something"方法

采纳答案by zwacky

instead of manually setting it through configs you could use Request::secure()to check, if the request is done over HTTPS.

您可以Request::secure()用来检查请求是否通过 HTTPS 完成,而不是通过配置手动设置它。

reference: laravel request information

参考:laravel 请求信息

回答by Paulo Manrique

Tested on Laravel 5x, you can do like this:

在 Laravel 5x 上测试,你可以这样做:

asset('path_to_file', \App::environment() == 'production')

If you're in production, it will return true and load the asset via https, while returning false in development, loading via http.

如果您在生产中,它将返回 true 并通过 https 加载资产,而在开发中返回 false,通过 http 加载。

回答by JDA3

Set HTTPS 'on' or 'off' in your web server's environment. That should propagate to Laravel, and result in http: or https: URL generation.

在您的 Web 服务器环境中将 HTTPS 设置为“开”或“关”。这应该传播到 Laravel,并导致 http: 或 https: URL 生成。

It may be worth adding that we run our Laravel apps on Elastic Beanstalk. EB terminates SSL at the Load Balancer, so Laravel "thinks" it's HTTP, when it's not. We use redirects to ensure all traffic is HTTPS from the outside, and set HTTPS=ON in the EB Dashboard settings so that assets aren't subject to redirects.

值得补充的是,我们在 Elastic Beanstalk 上运行我们的 Laravel 应用程序。EB 在负载均衡器上终止 SSL,因此 Laravel “认为”它是 HTTP,而实际上它不是。我们使用重定向来确保所有流量都是来自外部的 HTTPS,并在 EB 仪表板设置中设置 HTTPS=ON 以便资产不受重定向的影响。

回答by Hoshomoh

If you are using Laravel 5.3 you can just use the Laravel asset()helper function. It loads assets using the current scheme of the request (HTTP or HTTPS) You can find more details here

如果您使用的是 Laravel 5.3,您可以只使用 Laravelasset()辅助函数。它使用请求的当前方案(HTTP 或 HTTPS)加载资产您可以在此处找到更多详细信息