首页 > 解决方案 > Symfony 5 不处理来自 framework.yaml 文件的会话配置

问题描述

我有 4 个框架文件:

/config/framework.yaml:

framework:
  csrf_protection: true

  session:
    # enables the support of sessions in the app
    enabled: true
    # ID of the service used for session storage.
    # NULL means that Symfony uses PHP default session mechanism
    handler_id: Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler
    # improves the security of the cookies used for sessions
    cookie_secure: true
    cookie_lifetime: 172800
    cookie_httponly: true
    cookie_samesite: 'strict'

/config/package/framework.yaml

# see https://symfony.com/doc/current/reference/configuration/framework.html
framework:
    secret: '%env(APP_SECRET)%'
    csrf_protection: true
    #http_method_override: true

    # Enables session support. Note that the session will ONLY be started if you read or write from it.
    # Remove or comment this section to explicitly disable session support.
    session:
        handler_id: Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler
        cookie_secure: true
        cookie_lifetime: 172800
        cookie_httponly: true
        cookie_samesite: 'strict'

    #esi: true
    #fragments: true
    php_errors:
        log: true

\config\routes\dev\framework.yaml

_errors:
    resource: '@FrameworkBundle/Resources/config/routing/errors.xml'
    prefix: /_error

和 /config/packages/test/framework.yaml

framework:
    test: true
    session:
        storage_id: session.storage.mock_file

我看到没有使用 cookie 设置和会话处理程序:例如,创建的 cookie 的生命周期错误,并且会话未存储在 Redis 中。

我通过执行bin/console config:dump-reference framework命令检查了应用程序状态,我看到定义的配置不存在:

    session:
        enabled:              false
        storage_id:           session.storage.native
        handler_id:           session.handler.native_file
        name:                 ~
        cookie_lifetime:      ~
        cookie_path:          ~
        cookie_domain:        ~
        cookie_secure:        ~ # One of true; false; "auto"
        cookie_httponly:      true
        cookie_samesite:      null # One of null; "lax"; "strict"; "none"
        use_cookies:          ~
        gc_divisor:           ~
        gc_probability:       1
        gc_maxlifetime:       ~
        save_path:            '%kernel.cache_dir%/sessions'

        # seconds to wait between 2 session metadata updates
        metadata_update_threshold: 0
        sid_length:           ~
        sid_bits_per_character: ~

为什么?如何解决?

标签: phpsymfonysessioncookies

解决方案


推荐阅读