8

Use asset helper function like below:

<script src="{{ asset('/assets/js/jquery-2.1.1.min.js') }}"></script>

will get

<script src="http://xxx.xxx.com/assets/js/jquery-2.1.1.min.js"></script>

Is there any laravel build-in solutions can let it be

<script src="//xxx.xxx.com/assets/js/jquery-2.1.1.min.js"></script>

and properly display with http and https protocols?

[EDIT]

I know that Laravel normally detects the protocol correctly, but when behind a load balancer, it does not. So I'm still looking for a solution to this.

kramer65
  • 50,427
  • 120
  • 308
  • 488
Chen-Tsu Lin
  • 22,876
  • 16
  • 53
  • 63
  • Did you find a solution to this? – kramer65 Oct 07 '16 at 09:10
  • You realize the `asset` helper has a second argument `$secure`? That means `{{ asset('/assets/js/jquery-2.1.1.min.js', true) }}` should give you want you want, if you know the protocol needs to be https. – jszobody Oct 07 '16 at 14:49

8 Answers8

5

Laravel create secured link only if the request is considered as secured.

Laravel has already a way to treat non httpS traffic as "secured" if it's comming from a "trusted proxies".

You can declare such trusted proxies in your application service provider like that:

 Request::setTrustedProxies(array( '199.27.128.0/21', 'some other range'));

Also, make sure your loadbalancer set the following headers correctly:

Host, X-Forwarded-Host, X-Forwarded-Port, X-Real-IP, X-Forwarded-For and X-Forwarded-Proto

"Public" proxies such as cloudflare already does this btw.

Basically the point here is that client connection is terminated at one of the frontend servers (acting as a proxy), thus we have to declare communication from that server as trusted so that laravel use headers from the proxy server instead of the values readed localy.

Doing this enable functions such as Request::isSecure() or Request::ip() to return a consistent result.

Atrakeur
  • 4,126
  • 3
  • 17
  • 22
1

Laravel will automaticly change the url of the asset to the protocol used to load the site.

If your site is loaded over a secure connection, the asset links will automaticly use https.

Update:

If you do want to display the assets with only //, you could write your own HTML macro.

Jerodev
  • 32,252
  • 11
  • 87
  • 108
  • The problem is that if a site is behind a load balancer, it shows http links where it should be https links. – kramer65 Oct 07 '16 at 09:10
1

I had the same issue, then I discovered that the asset() method has an optional second variable, true or false for SSL requests.

The solution that I use is the following:

<link rel="stylesheet" href="{{ asset('assets/bootstrap/3.3.7/css/bootstrap.min.css', !App::isLocal()) }}" />

<script type="text/javascript" src="{{ asset('assets/parsley/2.4.4/parsley.min.js', !App::isLocal()) }}"></script>

Notice:

!App::islocal()

If my application environment is local, SSL isn't chosen, however in the production environment the assets will be called over SSL.

Sledmore
  • 300
  • 2
  • 10
0

The code that generates asset URLs is in illuminate/Routing/UrlGenerator.php

There is a forceSchema() method which could be easily modified to do what you require, though to be honest this functionality should probably be in Laravel and it may be worth submitting a pull request on their github.

Joe
  • 4,618
  • 3
  • 28
  • 35
0

You should check this article http://ankitpokhrel.com/explore/overriding-base-url-in-laravel-5/. basically you can override url() function in your AppServiceProvider to generate urls like /assets/something.js instead of http://example.com/assets/something.js

Kliment
  • 2,250
  • 3
  • 18
  • 32
0

asset() generate a URL for an asset using the current scheme of the request (HTTP or HTTPS):

And if you want to provide an external URL for which you don't know about http or https like cdn URL you can write like below

<script src="{!! asset('//code.jquery.com/jquery-2.1.1.min.js') !!}"></script>

However you can write like

<script src="//{!! Request::server ('HTTP_HOST').'/assets/js/jquery-2.1.1.min.js' !!}"></script>

OR

<script src="{!! asset('//'.Request::server ('HTTP_HOST').'/assets/js/jquery-2.1.1.min.js') !!}"></script>
Rakesh Sojitra
  • 3,538
  • 2
  • 17
  • 34
0

I normally use

<script src="/assets/js/jquery-2.1.1.min.js"></script>

without calling the asset() function

dirty I know, but in 90% of the sites I worked, it is a perfect solution

Luca C.
  • 11,714
  • 1
  • 86
  • 77
0

This might help someone.

Helper function way :

function schemalessAsset($path)
{
    if (url()->isValidUrl($path)) {
        return str_replace(['http:','https:'], '', $path);
    }

    return url()->assetFrom(url()->formatRoot('//'), $path);
}

Macroable way :

URL::macro('schemalessAsset', function ($path) {
    if (URL::isValidUrl($path)) {
        return str_replace(['http:','https:'], '', $path);
    }

    return URL::assetFrom(URL::formatRoot('//'), $path);
});

Tested in Laravel 5.6. It should work in other versions as well.

Ijas Ameenudeen
  • 9,069
  • 3
  • 41
  • 54