Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching Laravel assets between http and https in local and production

I'm developing an application that will eventually need to be fully HTTPS but I'm temporarily developing locally on 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.

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).

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

like image 552
alexleonard Avatar asked Apr 03 '14 05:04

alexleonard


3 Answers

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

reference: laravel request information

like image 187
Simon Wicki Avatar answered Sep 19 '22 11:09

Simon Wicki


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

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.

like image 31
JDA3 Avatar answered Sep 19 '22 11:09

JDA3


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

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.

like image 22
Paulo Manrique Avatar answered Sep 21 '22 11:09

Paulo Manrique