Skip to content

Symfony Session doesn't work if set handler_id via environment variable #45202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
linuxitux opened this issue Jan 27, 2022 · 6 comments
Closed

Comments

@linuxitux
Copy link

linuxitux commented Jan 27, 2022

Symfony version(s) affected

5.3.0/5.4.5

Description

Symfony throws Unsupported Connection: "session.handler.native_file" if you set handler_id in config/packages/framework.yaml to session.handler.native_file via .env variable.

I want to have the ability to switch between different session handlers in my application, so I added a .env variable named "SESSION_HANDLER" to set handler_id in framework.yaml. I want to support several session handlers, including native_file.

If I set a Redis URL in my SESSION_HANDLER .env var, it works perfectly and uses my Redis node as the session handler. But, if I set SESSION_HANDLER to session.handler.native_file, I got:

InvalidArgumentException

Unsupported Connection: "session.handler.native_file".

In vendor/symfony/http-foundation/Session/Storage/Handler/SessionHandlerFactory.php  (line 85) .

How to reproduce

Add SESSION_HANDLER var in .env and set it to session.handler.native_file:

.env:

SESSION_HANDLER=session.handler.native_file

Set handler_id via .env var in framework.yaml:

config/packages/framework.yaml:

framework:
    session:
        handler_id: '%env(SESSION_HANDLER)%'

internal_server_error.txt

Possible Solution

No response

Additional Context

No response

@linuxitux
Copy link
Author

Tested in 5.4.5. Same issue.

@alexander-schranz
Copy link
Contributor

You can not switch the session handler based on an env variable as it can not be changed at runtime, it requires a whole cache clear. You would need to switch to a parameter instead of env var.

There are some discussion about static vs runtime env vars here: #40794

@xabbuh
Copy link
Member

xabbuh commented Apr 13, 2022

Closing here as explained by @alexander-schranz. Thank you for understanding.

@xabbuh xabbuh closed this as completed Apr 13, 2022
@linuxitux
Copy link
Author

@alexander-schranz thanks for your reply. The whole purpose of setting the session handler via an env var is to keep this setting variable and isolated from the version control software. If I set the session handler using a parameter in framework.yaml I would have to keep the entire file away from my control version system. I need to keep framework.yaml tracked by git with the ability to switch between session handlers without messing with deploys. Is that possible? Thanks again.

@alexander-schranz
Copy link
Contributor

alexander-schranz commented Apr 19, 2022

It is not possible as env variables are designed to be used only for configurations which do not require a cache clear. For using env variables for configs which do require a cache clear the linked issue is about it (#40794).

You could still use the parameters.yml/parameters.yml.dist like it was in symfony 3/2: see comment here: #40794 (comment)

See also the linked presentation here where nicolas did talk about runtime parameters and config parameters and so on: #40794 (comment)

As you see the last comment of the linked issue is "Help wanted", so symfony is open here for a pull request to introduce a special env resolver to achieve this things e.g.: #40794

Until you need as said go with something else, a parameters.yml or some hack in your Kernel.php to set a kernel parameter by $_SERVER/$_ENV variable to achieve what you want. Or use the .php config instead of .yml and use $_SERVER or $_ENV to achieve what you want.

@linuxitux
Copy link
Author

@alexander-schranz thank you very much. I will be back on this if I find a proper solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants