Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 6 - Generate URL with "https" on twig templates

I want to generate URL with https:// as a prefix, using the Twig function url(). However, the generated URL is always http://.

I made the setup given here: https://symfony.com/doc/6.2/routing.html#forcing-https-on-generated-urls.

My config/packages/framework.yaml:

framework:                                                                                                                                                                             
    http_method_override: false                                                                             
    handle_all_throwables: true                                                                             
    ...

My config/routes.yaml:

controllers:                                         
    resource:                                        
        path: ../src/Controller/                     
        namespace: App\Controller                    
    type: attribute                                  
    defaults: [https] // note HTTPS here

I am using the PROD environment.

My .env file is:

APP_ENV=prod
APP_URL=https://something... // note HTTPS here

Cache has been cleared with prod option.

However, when I use from my twig template:

{{ url('my_route') }}

the generated route is correct but still in http (and not https).

Am I missing something ?

like image 275
jean553 Avatar asked Jul 26 '26 18:07

jean553


1 Answers

Looking here: https://symfony.com/doc/current/routing.html#forcing-https-on-generated-urls you will see you missed the schemes key in your config. Here is the example given:

# config/routes/annotations.yaml
controllers:
    resource: '../../src/Controller/'
    type: annotation
    defaults:
        schemes: [https]

Or do the following on a per route basis:

    #[Route('/login', name: 'login', schemes: ['https'])]
like image 182
Arleigh Hix Avatar answered Jul 29 '26 15:07

Arleigh Hix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!