du5407
2018-08-11 01:46
浏览 168
已采纳

你如何在Symfony 4中使用RedisSessionHandler? 破碎的文档

The documentation is broken and leads to a 404 page. I'm not too good at reverse engineering classes like this, any tips on how to setup services.yml to use it?

Doc page https://symfony.com/doc/current/components/http_foundation/session_configuration.html

Session Handler 404 https://api.symfony.com/4.1/Symfony/Component/HttpFoundation/Session/Storage/Handler/RedisSessionHandler.html

Git Page for Session Handler https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/RedisSessionHandler.php

图片转代码服务由CSDN问答提供 功能建议

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douyan1972 2018-08-14 14:52
    已采纳

    You need to define 2 services:

    • One for the Redis connection.
    • One for RedisSessionHandler that will make use of that connection.

    Edit the services file:

    # config/services.yaml
    services:
        Redis:
            class: Redis
            calls:
                - method: connect
                  arguments:
                      - '%env(REDIS_HOST)%'
                      - '%env(int:REDIS_PORT)%'
                # If you need key prefix
                # - method: setOption
                #   arguments:
                #       - !php/const Redis::OPT_PREFIX
                #       - 'my_prefix'
    
        Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler:
            arguments:
                - '@Redis'
    

    (note that I stored here the Redis host & port as environment variables, but you can define them elsewhere if needed).

    You can now make use of the service as your session handler:

    # config/packages/framework.yaml
    framework:  
        session:
            handler_id: Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler
    

    I struggled a bit too, and wrote a more detailed articled: http://blog.michaelperrin.fr/2018/08/14/redis-session-handling-in-symfony/

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douzhan8395 2019-01-30 09:52

    For adding custom prefix, maybe the following is better:

        Redis:
            class: Redis
            calls:
                - method: connect
                  arguments:
                      - '%env(REDIS_HOST)%'
                      - '%env(int:REDIS_PORT)%'
    
    #            - method: setOption
    #              arguments:
    #                  # @see https://symfony.com/blog/new-in-symfony-3-2-php-constants-in-yaml-files
    #                  - !php/const Redis::OPT_PREFIX
    
        Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler:
            arguments:
                - '@Redis'
                # @see https://symfony.com/doc/current/components/yaml/yaml_format.html
                -
                    prefix: ivannotes_
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题