Skip to content

[5.x]: REDIS error following docs #18236

@jlabs

Description

@jlabs

What happened?

Description

Setting up a site to use REDIS for cache storage and get an error when following the docs.

I add (as per docs) the following to app.php:

'components' => [
        'cache' => function() {
            $config = [
                'class' => yii\redis\Cache::class,
                'keyPrefix' => Craft::$app->id,
                'defaultDuration' => Craft::$app->config->general->cacheDuration,

                // Full Redis connection details:
                'redis' => [
                    'hostname' => App::env('REDIS_HOSTNAME') ?: 'localhost',
                    'port' => 6379,
                    'password' => App::env('REDIS_PASSWORD') ?: null,
                ],
            ];

            return Craft::createObject($config);
        },
    ],

Then get the error detailed below.

Discussing with the fellow AI, the suggestion is to use this instead:

'components' => [
        'redis' => [
            'class' => yii\redis\Connection::class,
            'hostname' => App::env('REDIS_HOSTNAME') ?: 'localhost',
            'port' => App::env('REDIS_PORT') ?: 6379,
            'password' => App::env('REDIS_PASSWORD') ?: null,
            'database' => App::env('REDIS_DB') ?: 0,
        ],
        'cache' => [
            'class' => yii\redis\Cache::class,
            'redis' => 'redis',
            'defaultDuration' => 86400,
        ],
    ],

Steps to reproduce

  1. Install yii2-redis as per docs
  2. Configure app.php as per docs

Expected behavior

No error

Actual behavior

Error returned on every page:

An Error occurred while handling another error:
yii\di\NotInstantiableException: Can not instantiate yii\redis\ConnectionInterface. in /project/location/project-name/vendor/yiisoft/yii2/di/Container.php:408

Craft CMS version

5.8.20

PHP version

8.4

Operating system and version

MacOS 15.7.3 and Ubuntu 24

Database type and version

MySQL 8.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions