php 在 symfony 2 中访问 AppKernel 环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10640866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing the AppKernel environment variable in symfony 2
提问by ContextSwitch
I'm Using symfony 2 and we have 2 configurations, dev and prod. I need to know if I can find out which one im using inside an Entity or Model.
我正在使用 symfony 2,我们有 2 个配置,dev 和 prod。我需要知道我是否能找出我在实体或模型中使用的是哪一个。
I'm looking for something similar to this code found in AppKernel.php:
我正在寻找类似于在 AppKernel.php 中找到的此代码的内容:
$this->getEnvironment()
If I could load the Kernel to call this that would be great but I can't find a way to do this. After looking into this it appears that symfony events may return the Kernel but I don't know how or where to capture these events so that I can call getKernel() on them. http://symfony.com/doc/current/book/internals.html
如果我可以加载内核来调用它,那就太好了,但我找不到办法做到这一点。在调查之后,symfony 事件似乎可能返回内核,但我不知道如何或在哪里捕获这些事件,以便我可以对它们调用 getKernel()。http://symfony.com/doc/current/book/internals.html
For example, they list this example:
例如,他们列出了这个例子:
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
使用 Symfony\Component\HttpKernel\Event\FilterControllerEvent;
public function onKernelController(FilterControllerEvent $event)
{
$controller = $event->getController();
// ...
// the controller can be changed to any PHP callable
$event->setController($controller);
}
Its unclear to me where to put this block of code. It seems to me that it should go in the Kernel, and if I had the Kernel I wouldn't be having this problem.
我不清楚把这段代码放在哪里。在我看来它应该放在内核中,如果我有内核,我就不会遇到这个问题。
My question is, is there an easy way for me to determine if I'm in 'dev' or 'prod' as set in the Kernel, from a Service or Model. Thanks
我的问题是,是否有一种简单的方法可以让我从服务或模型中确定我是处于内核中设置的“开发”还是“生产”。谢谢
回答by Peter Bailey
The default entity classes generated by the console don't inherit anything. This means they aren't "ContainerAware" in any way.
控制台生成的默认实体类不继承任何东西。这意味着它们在任何方面都不是“ContainerAware”。
And generally speaking, I don't think they should be. I supposed it depends on what you're doing but you could handle this with some basic dependency injection
一般来说,我认为它们不应该是。我想这取决于你在做什么,但你可以用一些基本的依赖注入来处理这个
In a controller:
在控制器中:
$entity = new \Your\Bundle\Entity\Foo(
$this->container->get( 'kernel' )->getEnvironment()
);
And then in src/Your/Bundle/Entity/Foo.php
然后在src/Your/Bundle/Entity/Foo.php
private $env;
public function __construct( $env=null )
{
$this->env = $env;
}
Would this work for you?
这对你有用吗?
P.S. The event listener you posted about is for Controllers - not for arbitrary classes.
PS 您发布的事件侦听器适用于控制器 - 不适用于任意类。
回答by Francesco Casula
It's also possible to get that as a parameter. If you take a look at the \Symfony\Component\HttpKernel\Kernelclass you'll find a getKernelParameters()method that exposes all the kernel parameters.
也可以将其作为参数获取。如果您查看\Symfony\Component\HttpKernel\Kernel该类,您会发现一个getKernelParameters()公开所有内核参数的方法。
/**
* Returns the kernel parameters.
*
* @return array An array of kernel parameters
*/
protected function getKernelParameters()
{
$bundles = array();
foreach ($this->bundles as $name => $bundle) {
$bundles[$name] = get_class($bundle);
}
return array_merge(
array(
'kernel.root_dir' => realpath($this->rootDir) ?: $this->rootDir,
'kernel.environment' => $this->environment,
'kernel.debug' => $this->debug,
'kernel.name' => $this->name,
'kernel.cache_dir' => realpath($this->getCacheDir()) ?: $this->getCacheDir(),
'kernel.logs_dir' => realpath($this->getLogDir()) ?: $this->getLogDir(),
'kernel.bundles' => $bundles,
'kernel.charset' => $this->getCharset(),
'kernel.container_class' => $this->getContainerClass(),
),
$this->getEnvParameters()
);
}
So in a services.ymlfile you can get the environment with %kernel.environment%whilst in a container aware class you can get it by doing:
因此,在services.yml文件中,您可以获得环境,%kernel.environment%而在容器感知类中,您可以通过执行以下操作来获得环境:
$this->getContainer()->getParameter('kernel.environment');
回答by Steve Childs
Of course there is the quick and dirty way of globals...
当然有全局变量的快速而肮脏的方式......
function quickAndDirty() {
global $kernel;
if ($kernel->getEnvironment() == 'dev') {
// we're in dev mode
}
}
Its bad and evil and you should wash yourself after using it, but in the case of a large existing codebase that you perhaps inherited, it saves a potential refactoring nightmare.
它的坏处和邪恶,你应该在使用它后清洗自己,但在你可能继承的大型现有代码库的情况下,它避免了潜在的重构噩梦。
Of course, whether you can live with yourself after using such a method, is up to you ;)
当然,用了这样的方法后,你能不能和自己相处,就看你自己了;)
回答by Henry
(Note: this works on Symfony 3.x, not sure about 4.x)
(注意:这适用于 Symfony 3.x,不确定是否适用于 4.x)
You can inject %kernel.environment%straight into your service:
您可以%kernel.environment%直接注入您的服务:
my_service:
class: My\Foo
properties:
env: '%kernel.environment%'
Then in your service class:
然后在您的服务类中:
class Foo {
$env;
...
function someFunction()
{
if($this->env === 'dev') {
// do some dev stuff
}
else {
// do some prod stuff
}
}
}
This has the advantage that if you are unit testing you don't need the container.
这样做的好处是,如果您进行单元测试,则不需要容器。
If you don't like property injection, you can use constructor or setter injection.
如果你不喜欢属性注入,你可以使用构造函数或 setter 注入。

