Home » Php » php – Laravel: Set asset without protocol

php – Laravel: Set asset without protocol

Posted by: admin July 12, 2020 Leave a comment

Questions:

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.

How to&Answers:

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.

Answer:

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.

Answer:

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.

Answer:

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

Answer:

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>

Answer:

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.

Answer:

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

Answer:

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.