Application Configuration

You can customize Craft’s entire Yii application configuration from config/app.php. Any items returned by that array will get merged into the main application configuration array.

You can also customize Craft’s application configuration for only web requests or console requests from config/app.web.php and config/app.console.php.

TIP

Craft’s default configuration is defined by src/config/app.php, app.web.php, and app.console.php. Refer to these files when you need to override existing application components.

Cache Component

By default, Craft will store data caches in the storage/runtime/cache/ folder. You can configure Craft to use alternative cache storage by overriding the cache application component from config/app.php.

<?php
return [
    'components' => [
        'cache' => [
            'class' => yii\caching\ApcCache::class,
            'useApcu' => true,
        ],
    ],
];

Memcached Example

<?php
return [
    'components' => [
        'cache' => [
            'class' => yii\caching\MemCache::class,
            'useMemcached' => true,
            'username' => getenv('MEMCACHED_USERNAME'),
            'password' => getenv('MEMCACHED_PASSWORD'),
            'defaultDuration' => 86400,
            'servers' => [
                [
                    'host' => 'localhost',
                    'persistent' => true,
                    'port' => 11211,
                    'retryInterval' => 15,
                    'status' => true,
                    'timeout' => 15,
                    'weight' => 1,
                ],
            ],
        ],
    ],
];

Redis Example

To use Redis cache storage, you will first need to install the yii2-redis library. Then configure Craft’s cache component to use it:

<?php
return [
    'components' => [
        'redis' => [
            'class' => yii\redis\Connection::class,
            'hostname' => 'localhost',
            'port' => 6379,
            'password' => getenv('REDIS_PASSWORD'),
        ],
        'cache' => [
            'class' => yii\redis\Cache::class,
            'defaultDuration' => 86400,
        ],
    ],
];

Session Component

In a load-balanced environment, you may want to override the default session component to store PHP session data in a centralized location (e.g. Redis):

<?php
return [
    'components' => [
        'redis' => [
            'class' => yii\redis\Connection::class,
            'hostname' => 'localhost',
            'port' => 6379,
            'password' => getenv('REDIS_PASSWORD'),
        ],
        'session' => [
            'class' => yii\redis\Session::class,
            'as session' => craft\behaviors\SessionBehavior::class,
        ],
    ],
];

TIP

The session component must be configured with the craft\behaviors\SessionBehavior behavior, which adds methods to the component that the system relies on.

Mailer Component

To override the mailer component config (which is responsible for sending emails), do this in config/app.php:

<?php
return [
    'components' => [
        'mailer' => function() {
            // Get the stored email settings
            $settings = Craft::$app->systemSettings->getEmailSettings();

            // Override the transport adapter class
            $settings->transportType = craft\mailgun\MailgunAdapter::class;

            // Override the transport adapter settings
            $settings->transportSettings = [
                'domain' => 'foo.com',
                'apiKey' => 'key-xxxxxxxxxx',
            ];

            // Create a Mailer component config with these settings
            $config = craft\helpers\App::mailerConfig($settings);
            
            // Instantiate and return it
            return Craft::createObject($config);
        },
    ],
];

TIP

Any changes you make to the Mailer component from config/app.php will not be reflected when testing email settings from Settings → Email.

Queue Component

Craft’s job queue is powered by the Yii2 Queue Extension. By default Craft will use a custom queue driver based on the extension’s DB driver, but you can switch to a different driver by overriding Craft’s queue component from config/app.php:

<?php
return [
    'components' => [
        'queue' => [
            'class' => yii\queue\redis\Queue::class,
            'redis' => 'redis', // Redis connection component or its config
            'channel' => 'queue', // Queue channel key
        ], 
    ],
];

Available drivers are listed in the Yii2 Queue Extension documentation.

WARNING

Only drivers that implement craft\queue\QueueInterface will be visible within the Control Panel.

TIP

If your queue driver supplies its own worker, set the runQueueAutomatically config setting to false in config/general.php.

Modules

You can register and bootstrap custom Yii modules into the application from config/app.php as well. See How to Build a Module for more info.