无法在 Phoenix 控制器 and/or 模板中获取配置值
Trouble getting config values within Phoenix controller and/or template
我正在尝试从控制器中的 Phoenix 配置文件中获取值。
# config.exs
use Mix.Config
config :app_name, AppName.Endpoint,
url: [host: "localhost"],
debug_errors: false,
root: Path.expand("..", __DIR__)
例如Application.get_env(:app_name, :url)
,好像return没什么。
我是不是漏掉了什么?
正如您在 docs for the Mix.Config
module 中看到的,config
有两个变体:config/2
和 config/3
。您在传递 三个参数时使用 config/3
变体:
:app_name
AppName.Endpoint
- 关键字列表 (
[url: ..., debug_errors: ...]
)
这意味着您正在 :app_name
应用程序环境中配置 AppName.Endpoint
键,并将其值设置为关键字列表(记住 AppName.Endpoint
只是一个原子,因此可以将其用作密钥)。要检索 url,您需要执行以下操作:
Application.get_env(:app_name, AppName.Endpoint)[:url]
为了完整起见,config/2
允许在一个应用程序的环境中设置多个键值对。它的参数实际上是应用程序名称和键值对列表。
我正在尝试从控制器中的 Phoenix 配置文件中获取值。
# config.exs
use Mix.Config
config :app_name, AppName.Endpoint,
url: [host: "localhost"],
debug_errors: false,
root: Path.expand("..", __DIR__)
例如Application.get_env(:app_name, :url)
,好像return没什么。
我是不是漏掉了什么?
正如您在 docs for the Mix.Config
module 中看到的,config
有两个变体:config/2
和 config/3
。您在传递 三个参数时使用 config/3
变体:
:app_name
AppName.Endpoint
- 关键字列表 (
[url: ..., debug_errors: ...]
)
这意味着您正在 :app_name
应用程序环境中配置 AppName.Endpoint
键,并将其值设置为关键字列表(记住 AppName.Endpoint
只是一个原子,因此可以将其用作密钥)。要检索 url,您需要执行以下操作:
Application.get_env(:app_name, AppName.Endpoint)[:url]
为了完整起见,config/2
允许在一个应用程序的环境中设置多个键值对。它的参数实际上是应用程序名称和键值对列表。