So I'm sometimes developing at home on my vagrant homestead box (which is HTTP) and sometimes on the road via c9.io (which is HTTPS). If I put this in my whatever.blade.php file:
<link href="{{ asset('/css/style.css') }}" rel="stylesheet">
...then the resulting URL is http://localhost:8000/css/style.css
, which works fine when my page is being served over HTTP but not when it's being served over HTTPS. If I do this:
<link href="{{ asset('/css/style.css', true) }}" rel="stylesheet">
then the URL is HTTPS so it works when the page is HTTPS but breaks when the page is HTTP. I tried doing this:
<link href="{{ asset( '/css/style.css', Request::secure() ) }}" rel="stylesheet">
...but apparently Request::secure()
doesn't work anymore in Laravel 5.0. :-(
Does anyone have any elegant suggestions on how to make assets work smoothly via HTTPS when the page is served over HTTPS, but just regular HTTP when the page is served over HTTP?
Thanks.
I guess I could put APP_SECURE=true
or APP_SECURE=false
in my .env
file, and reference that in every single asset
call ... that might be the best I can do. Doesn't feel elegant and Laravel-esque, though.
Try putting HTTPS=on or HTTPS=off in your .env file. No other editing should be required.
Maybe it will be helpful. Laravel automatically detect HTTPS and make asset schema by request protocol (just checks $_SERVER['HTTPS']
). However, I faced the problem, that didn't work in my hosting. It was caused by using combination of apache and ngnix that changes HTTP headers. As a result, $_SERVER['HTTPS']
was renamed to $_SERVER['HTTP_HTTPS']
, so Laravel couldn't detect HTTPS. In this case, you can write $_SERVER['HTTPS']
via PHP before detection (e.g. in public/index.php
) or set schema via app('url')->forceSchema(...)
.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community