在 yml 文件中使用 PHP 全局变量 [英] Using PHP global variable inside yml file
问题描述
我正在尝试在 yml 文件中使用 $_SERVER
全局变量,但我无法在任何地方找到如何使用 PHP 执行此操作.这样做的所有在线示例都是针对 ruby 的.可以用 PHP 吗?
I'm trying to use the $_SERVER
global variable inside the yml file but I cant find anywhere how to do it with PHP. All the online examples to doing so are for ruby. Is it possible with PHP?
推荐答案
您可以像使用 $_GET
和 $_POST
一样获取服务器变量.
You can get the server variables the same way you would with $_GET
and $_POST
.
$request->server->get('YOUR_VARIABLE') // For a specific $_SERVER variable
$request->server->all() // For all $_SERVER variables
更新:
要将它们放入配置文件中,您可以执行以下操作(取自 Openshift Symfony2 发行版 - https://github.com/hasinhayder/openshift-symfony-2.3.0/tree/master/php/app/config)
To get them in the config files you could do the following (taken from an Openshift Symfony2 distribution - https://github.com/hasinhayder/openshift-symfony-2.3.0/tree/master/php/app/config)
在一个 php 文件中设置你的参数,在上面的例子中它被称为 params.php
Set your parameters in a php file, in the example above it's called params.php
<?php
$container->setParameter('parameter_one', $_SERVER['parameter_one']);
然后将其包含在您的 config.yml 文件中
Then include that in your config.yml file
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: params.php }
然后你应该能够使用它们,就好像它们已经被设置在一个常规的链接 yml 文件中一样
And then you should just be able to use them as if they had been set in a regular linked yml file like
%parameter_one%
这篇关于在 yml 文件中使用 PHP 全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!