Custom configuration in phoenix framework

1,055 views
Skip to first unread message

rob...@rego.lv

unread,
Jan 19, 2016, 10:34:35 AM1/19/16
to phoenix-talk
Hello, how can i create custom configuration file in config directory and then read it where needed.

Desirable at compile time.

Didn't find in phoenix guides about, how phoenix packages configurations are/should be handled?


benwil...@gmail.com

unread,
Jan 19, 2016, 1:44:09 PM1/19/16
to phoenix-talk
Phoenix is configured the way every elixir application is configured: with Mix config. Is there some specific thing you want to do that you aren't sure how to do with Mix?

rob...@rego.lv

unread,
Jan 19, 2016, 3:30:58 PM1/19/16
to phoenix-talk, benwil...@gmail.com
I am new to phoenix and elixir so it may be that i am missing something.

For example, i want to create new config file with in config/ directory. How could i than access that config? I phoenix guides i can't find answer. 

I found PhoenixCqrs.Endpoint.config(:handlers) and it works in my routes file, but in my lib it throws 
** (ArgumentError) argument error
    (stdlib) :ets.lookup(PhoenixCqrs.Endpoint, :handlers)

So what is the right way how to create custom configs and access them, both at run time and compile time?

Thanks.

Steve Domin

unread,
Jan 25, 2016, 8:56:53 AM1/25/16
to phoenix-talk, benwil...@gmail.com, rob...@rego.lv
Hello,

You can define new config in config/config.exs, like this:

config :custom_config, key: "value"

Then in your app you can access it this way:

custom_config = Application.get_env(:custom_config, :key)

If you want to put your config in a separate file you can as well, you just need to import it inside config/config.exs using the `import_config` macro.

# in config/config.exs

import_config
"custom_config.exs"

Then you can use it in the same way in your app.

You can read more about this in the Mix docs: http://elixir-lang.org/docs/stable/mix/Mix.Config.html

rob...@rego.lv

unread,
Jan 27, 2016, 12:45:54 PM1/27/16
to phoenix-talk, benwil...@gmail.com, rob...@rego.lv
Thanks :)
Reply all
Reply to author
Forward
0 new messages