Laravel not getting the APP_URL parameter when we try to use url function inside the blade file

350 Views Asked by At

I'm facing a weird situation, just look at the following code in a blade file, and the output of it

Blade

{{ url('xero_invoice_authorised') }}</br>
config app url - {{ config('app.url') }}</br>
env app url - {{ env('APP_URL') }}

Output

http://xxxx/xero_invoice_authorised
config app url - https://xxxx/
env app url - https://xxxx/

url() is returning the URL as http: but it should be https:.

The system is inside a docker container.

I have run all commands to clear the cache, view, routes, and even ran optimize command.

Can anyone suggest to me what's wrong with my configuration? or any method to rectify this issue?

I need {{ url('') }} to return the APP_URL in the .env file.

3

There are 3 best solutions below

1
On

After different tryouts found a working method I updated the AppServiceProvider with the following to force the HTTPS on production environment. File : app/Providers/AppServiceProvider.php

public function boot()
    {
            if (env('APP_ENV') === 'production') {
                URL::forceScheme('https');
            }
    }
0
On

Don't use env() anywhere except in your con fig files; once config has been cached, there will be nothing available from .env.

If AMP is a third party service, then put the key into config/services.php:

// config/services.php

<?php

return [
    // ...

    'amp' => [
        'key' => env('AMP_KEY'),
    ],

],

Then in the view:

<meta name="amp_key" content="{{ config('services.amp.key') }}">
1
On

There is no error at all, it is working as expected.

First of all, never use env() outside the config folder, because when you run php artisan optimize or php artisan config:cache, env() will always return null. More info in the documentation. I understand if this is just a test, but have it always in mind.

Second, url() will ALWAYS return http instead of https. You have to use secure_url() if you want https. Check the documentation for url() and secure_url().

Third, just to show you how it works, here is the official source code of url(), the UrlGenerator is the one processing the http:// or https://, you can try debugging what it is reading and checking why it is using insecure instead of the other one.