skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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');
                }
        }
    

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search