简体   繁体   English

Symfony2-将参数传递给服务

[英]Symfony2 - pass parameter to service

is there any way to pass parameter into service from SESSION or other source? 有什么方法可以将参数从SESSION或其他来源传递到服务中? I have something like this in my bundle configuration: 我的捆绑包配置中有以下内容:

<service id="class" class="MyClass">
    <argument key="lang">%class.lang%</argument>
</service>

I know that it can be done by passing parameter from Extension in DependencyInjection but I'm not sure about the source of this variable, it might be session but in some cases it could be readed from database. 我知道可以通过在DependencyInjection中传递来自Extension的参数来完成此操作,但是我不确定此变量的来源,它可能是会话,但在某些情况下可以从数据库中读取。 I just dont want to hardcode in DependencyInjection that parameter should be passed from session. 我只是不想在DependencyInjection中硬编码应该从会话传递参数。

I know also that I can pass parameter from app config but is there any way to change it after some acction in application? 我也知道我可以通过app config传递参数,但是在应用程序中执行某些操作后,是否有任何方法可以更改它?

如果该参数不是由配置确定的(即,它取决于某些应用程序逻辑),则将其放在特定的类中,将该类注册为特定的服务(注入所有必需的服务以确定您感兴趣的值),然后在需要该价值的地方注入特定的服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM