• Skip to main content
  • Skip to primary sidebar

Ryan McCormick

Dedicated Dad, Software Engineer and Lover of Coffee

SOLVED: Force Assets to use HTTPS in Laravel

July 21, 2017 by Ryan McCormick 2 Comments

I have been doing some work with Laravel lately and needed to force a site to use https for everything. One of the first problems I encountered was figuring out how to force https in Laravel for linked stylesheets, javascript assets, etc…

I found a few different solutions, but none of them solved the issue. I was able to combine a couple of different approaches and found one that worked really well.

HTTPS in Laravel | Setup Asset and URL Helpers

Set an environment variable in your https environments
Since most people develop in a localhost/non https environment, I found that setting a boolean environment variable takes away the pain. I called mine REDIRECT_HTTPS. I set the value to true.

Update your app/Providers/AppServiceProvider.php file
As you will see below, I check the environment variable to enable the https settings. If set to true, the https prefix is added to your asset urls.

namespace App\Providers;

use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot(UrlGenerator $url)
    {
        if(env('REDIRECT_HTTPS')) {
            $url->formatScheme('https');
        }
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        if(env('REDIRECT_HTTPS')) {
            $this->app['request']->server->set('HTTPS', true);
        }
    }
}

Import your assets normally
After your have everything in place, you can just import your assets normally and they will load with https in https environments and http in non-https environments depending on your environment variable.






Learn Laravel

Before I started working on this project, my knowledge of the Laravel framework was very limited. I worked through a few examples, but was still left with a lot of questions about facades, helpers, the dependency injection mechanism, database migrations, security, etc…

When learning a new framework, my goto is normally Pluralsight. I will be writing a post with more training resources than the following, but wanted to recommend the following courses:

  1. Getting Started with Laravel (PHP Framework) – The Basics
  2. Getting Started with Laravel (PHP Framework) – Models and Data
  3. RESTful Web Services with PHP and Laravel

As always, please comment with questions, etc…

Related

Filed Under: Laravel Tagged With: asset https, helpers, laravel, php

Reader Interactions

Comments

  1. Sata Lesmana says

    April 19, 2019 at 10:01 pm

    thank for sharing, you are saved my life

    Reply
  2. Tzvetan says

    November 15, 2019 at 4:19 am

    Two years after, still the best solution I found

    Reply

Leave a Reply Cancel reply

Primary Sidebar

Recent Posts

  • Force Quit Kill all Chrome Windows MacOS
  • SOLVED: Angular 6 CLI Karma Stuck in Single Run | Karma Stops Running
  • How to Manually Install Java 8 on Ubuntu 18.04 LTS
  • Remove VirtualBox from Ubuntu 16.04 Xenial
  • Clear all Node Modules Folders Recursively Mac/Linux

Recent Comments

  • KKV on Webstorm adding spaces between imports and braces | JavaScript and TypeScript
  • jusopi on Clear all Node Modules Folders Recursively Mac/Linux
  • Qaisar Irfan on Clear all Node Modules Folders Recursively Mac/Linux
  • mustafa on Remove VirtualBox from Ubuntu 16.04 Xenial
  • Pourya on How to Manually Install Java 8 on Ubuntu 18.04 LTS

Archives

  • May 2019
  • May 2018
  • April 2018
  • March 2018
  • January 2018
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • March 2017
  • December 2015
  • November 2015
  • July 2015
  • April 2015
  • February 2015
  • September 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • October 2013
  • August 2013
  • June 2013
  • April 2013
  • March 2013
  • February 2013
  • December 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • May 2012
  • March 2012
  • February 2012
  • December 2011
  • November 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • August 2009
  • July 2009
  • May 2009

Categories

  • Angular
  • Angular 2
  • AngularJS (1x branch)
  • Computer Q&A
  • ES2015
  • Internet Marketing
  • Javascript
  • Job Interviews
  • Job Search
  • Karma
  • Laravel
  • Linux
  • Linux/Unix Tips
  • MacOS
  • Microsoft Access
  • Microsoft Excel
  • Microsoft Outlook
  • Microsoft Word
  • News
  • Node
  • Open Source
  • PHP
  • Protractor
  • Resume Writing
  • Spring Boot
  • SQL
  • Ubuntu
  • VBA
  • VBScript
  • VirtualBox
  • Web Development
  • Windows Tips
  • Wordpress

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Copyright © 2023 · Magazine Pro on Genesis Framework · WordPress · Log in