salivan salivan - 17 days ago 12
PHP Question

laravel asset() method doesn't return https

I'm using

asset()
public method to generate correct url in laravel.

in docs it says:

enter image description here

So in theory it should detect correct scheme itself.

But in code I see: https://github.com/illuminate/routing/blob/master/UrlGenerator.php#L210

public function asset($path, $secure = null)
{


default for secure is null. So this method is no good for both http/https.

what am I missing here?

I'm using reverse proxy, could it because of that?

Answer

As you can see from GitHub the asset method is calling getScheme to determine what the scheme should be.

https://github.com/illuminate/routing/blob/master/UrlGenerator.php#L276

So if you don't provide the asset 2nd parameter $secure then it uses the request scheme. Otherwise you can provide $secure to force the desired scheme regardless of what is the scheme in the request.

If you look at the code you'll see that if $secure is null and no cache is set than the cache is set to the request scheme (i.e. $this->request->getScheme()) and therefore returned.