XF 2.1 Override options from config.php

PaulB

Well-known member
Is it possible to override options from config.php in XF2? For XF1 development, we patched XenForo to permit options to be overridden from config.php; we could then distribute a template config.php to our developers with sane defaults. However, this required us to maintain a variety of patches, since the options needed to be set before add-ons were installed. Has the situation changed with XF2?

For example, I noticed that \XF\Mailer\Mail::getTransportFromOption supports 'file' as a valid transport type to dump mail to files. Our developers need to be able to see what emails were sent by their add-ons, so toggling enableMail isn't a great option, although we could set it to false as a safe default. A better solution would be to set the emailTransport option to 'file', but I haven't found a way to do that from config.php without either a patch or an add-on.
 
Last edited:
You can extend options, but actually in this case you want to just override the transport container directly:
PHP:
/** @var $c \XF\Container */
$c->extend('mailer.transport', function()
{
    return \XF\Mail\Mailer::getTransportFromOption('file', [
        'path' => \XF::getRootDirectory() . '/internal_data'
    ]);
});

That will output all outgoing emails into the internal_data directory.

In cases where you might need to override option values at run time, the process is similar:

PHP:
$c->extend('options', function(\ArrayObject $options)
{
    $options->useFriendlyUrls = true;
    return $options;
});
 
Indeed, my preferred option. I even configure that directly in config.php too so I don't forget:

PHP:
/** @var $c \XF\Container */
$c->extend('mailer.transport', function()
{
   return \XF\Mail\Mailer::getTransportFromOption('smtp', [
      'smtpHost' => 'localhost',
      'smtpPort' => 1025,
      'smtpAuth' => 'none'
   ]);
});
 
Back
Top Bottom