Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Dynamically change config values


  • I have the SMTP auth info set in the email.php config file, but my goal is to set this dynamically as needed.

    I've tried a two methods without luck:

        \Config::set('email.default.smtp.username', 'example');


         $email = \Email::forge('default', array('driver' => 'smtp', 'smtp.username' => 'username', 'smtp.password' => 'password'));


    What is the correct way to do this?
  • The first one doesn't work because the config is only loaded when the Email class is loaded (which will overwrite your setting if that happens later then your set) and when it loads, it will immediately copy the config to an internal 'defaults' array, so after that the config is no longer used.

    You could pass the information in the forge() call's option array, but then you can't use 'dot-notation', it has to be a multi-dimensional array structure.

    The correct way of doing this is to use the 'setup groups' array in the config file. Add this to it:
    'myuser' => array(
        'driver' => 'smtp',
        'smtp' => array(
            'username' => 'username',
            'password' => 'password',
        ),
    ),

    And then use
    $email = \Email::forge('myuser');
    This will merge your specific 'myuser' settings with the 'default' settings, so you only have to add the values you want to override it in.
  • Thank you very much for the detailed info. I appreciate it.

    I think you proposed two working solutions, but I want to confirm, because only one would work for my needs.

    The solution about updating the config file and calling the config group wont work since I will be obtaining the parameters via another API.

    What I need to do, is pass variables to the forge method so that I can dynamically set the needed config values.

    I am trying to determine the correct way to do that.

    Is it possible to build a new config group outside of the config file and then pass it to the forge method?
  • HarroHarro
    Accepted Answer
    The option array passed to the forge() method has the same structure as the array in the config file, as they are merged when you forge.

    So either do
    $email = \Email::forge('default', array('smtp' => array('username' => 'username', 'password' => 'password')));
    or do
    $config = \Config::load('email', true);
    \Arr::set($config, 'setups.default.smtp.username', 'username');
    \Arr::set($config, 'setups.default.smtp.password', 'password');
    $email = \Email::forge();
    to load the email config first, update the defaults, and then create the Email instance.

    I would opt for one, it's more clean, it doesn't corrupt your defaults, and it makes it clear to anyone reading the code that you're creating an instance with custom config.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion