Silverstripe 3 配置条件
Silverstripe 3 Config Condition
我在 YAML 配置中挣扎 API,可以找到哪些文档 here。我的用例:
我有一个 class 具有 2 个“配置”属性
class Foo extends Object {
private static $host = null;
private static $port = null;
}
现在我需要每个环境不同的主机名,但端口相同。
假设,在开发环境中,$host 应该是 1.1.1.1,而在 live 环境中,ist 必须是 2.2.2.2。端口应设置为 10000;
所有其他部分应该对所有环境类型都有效
config.yml
(无效):
---
Name: myexcitingconfig
---
Only:
environment: 'dev'
---
Foo:
host: '1.1.1.1'
port: 10000
---
Only:
environment: 'live'
---
Foo:
host: '2.2.2.2'
port: 10000
---
Bar:
test: 'hooray'
在此示例中,“Foo”的所有属性均为空,仅设置了“Bar::test”。
Debug::dump(Config::inst()->get('Foo', 'host')); // => null
Debug::dump(Config::inst()->get('Foo', 'port')); // => null
Debug::dump(Config::inst()->get('Bar', 'test')); // => 'hooray'
我做错了什么?
我认为您的 YAML 文件的结构方式,您的实际配置指令被误解为 YAML 文档 "headers",并且您的基于环境的规则被解释为配置。你试过这样的东西吗?
---
Name: myexcitingconfig
---
# Global config here
Bar:
test: 'hooray'
---
Only:
environment: 'dev'
---
Foo:
host: '1.1.1.1'
port: 10000
---
Only:
environment: 'live'
---
Foo:
host: '2.2.2.2'
port: 10000
---
此外,当使用命名空间 类 时,您应该在 YAML 文件中写入完全限定的类名。因此,如果 Foo
在命名空间 My\Awesome\Module
中,您的配置条目将是:
My\Awesome\Module\Foo:
host: '2.2.2.2'
port: 10000
是的!似乎在 "only" 条件之后没有解析任何配置设置。
以下作品,但不是最后一个 "test2" 属性:
---
Name: myexcitingconfig
---
Bar:
test: 'hooray'
---
Only:
environment: 'dev'
---
Foo:
host: '1.1.1.1'
port: 10000
---
Only:
environment: 'live'
---
Foo:
host: '2.2.2.2'
port: 10000
---
Bar:
test2: 'hooray, also'
我在 YAML 配置中挣扎 API,可以找到哪些文档 here。我的用例:
我有一个 class 具有 2 个“配置”属性
class Foo extends Object {
private static $host = null;
private static $port = null;
}
现在我需要每个环境不同的主机名,但端口相同。 假设,在开发环境中,$host 应该是 1.1.1.1,而在 live 环境中,ist 必须是 2.2.2.2。端口应设置为 10000;
所有其他部分应该对所有环境类型都有效
config.yml
(无效):
---
Name: myexcitingconfig
---
Only:
environment: 'dev'
---
Foo:
host: '1.1.1.1'
port: 10000
---
Only:
environment: 'live'
---
Foo:
host: '2.2.2.2'
port: 10000
---
Bar:
test: 'hooray'
在此示例中,“Foo”的所有属性均为空,仅设置了“Bar::test”。
Debug::dump(Config::inst()->get('Foo', 'host')); // => null
Debug::dump(Config::inst()->get('Foo', 'port')); // => null
Debug::dump(Config::inst()->get('Bar', 'test')); // => 'hooray'
我做错了什么?
我认为您的 YAML 文件的结构方式,您的实际配置指令被误解为 YAML 文档 "headers",并且您的基于环境的规则被解释为配置。你试过这样的东西吗?
---
Name: myexcitingconfig
---
# Global config here
Bar:
test: 'hooray'
---
Only:
environment: 'dev'
---
Foo:
host: '1.1.1.1'
port: 10000
---
Only:
environment: 'live'
---
Foo:
host: '2.2.2.2'
port: 10000
---
此外,当使用命名空间 类 时,您应该在 YAML 文件中写入完全限定的类名。因此,如果 Foo
在命名空间 My\Awesome\Module
中,您的配置条目将是:
My\Awesome\Module\Foo:
host: '2.2.2.2'
port: 10000
是的!似乎在 "only" 条件之后没有解析任何配置设置。 以下作品,但不是最后一个 "test2" 属性:
---
Name: myexcitingconfig
---
Bar:
test: 'hooray'
---
Only:
environment: 'dev'
---
Foo:
host: '1.1.1.1'
port: 10000
---
Only:
environment: 'live'
---
Foo:
host: '2.2.2.2'
port: 10000
---
Bar:
test2: 'hooray, also'