This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

🔊 Web Growth Summit is here! Learn from industry experts on July 17-18, 2024. REGISTER NOW→

How to Setup Yii 2 With Redis Configuration

Updated on July 15, 2021

3 Min Read

In my previous article on Yii2, I discussed how to install and use elasticsearch with Yii. In another article, I demonstrated the capabilities of Yii2 framework through a simple to-do app. Today I will demonstrate how to use Redis as a cache and session handler for Yii2. In my previous articles on Redis, I have discussed the use of Redis as a cache with custom PHP sites and Redis for PHP session management.

Setup Yii2 With Redis Configuration

To install Redis on Cloudways, go through my article linked above.

Step 1: Installing Redis Extension of Yii 2

Open your composer.json file and add the following line in require section:

"yiisoft/yii2-redis": "~2.0.0"

Installing Redis Extension of Yii 2

Now in the console, run the following command to install the extension

composer update

Once the composer have completed installation, open Yii2 in the browser. Open Yii debug bar and locate installed extensions. You will find yii2-redis in the list.

Installing Redis Extension of Yii 2

This means that Redis has been successfully installed and is ready for use.

Step 2: Connecting Redis With Yii2

Next, connect Yii2 application with Redis. Head to config folder and open web.php. Add the following lines in the components section.

       'redis'         => [

           'class'    => 'yii\redis\Connection',

           'hostname' => 'localhost',

           'port'     => 6379,

           'database' => 0,


       'session'       => [

           'class' => 'yii\redis\Session',


       'cache'         => [

           'class' => 'yii\redis\Cache',


Once done, save the file. It is time to use the extension as the session handler.

Step 3: Using Redis As a Session Handler With Yii2

Now open your controllers folder and add a new controller in it. Let’s name it RedisController.php. Now open the file and place the following code in it.


namespace app\controllers;

use Yii;

use yii\filters\AccessControl;

use yii\web\Controller;

use yii\filters\VerbFilter;

class RedisController extends Controller


   public function actionIndex()



       $a = Yii::$app->session->getFlash('contactFormSubmitted');

       echo $a;



Now run the this controller in browser and you will see below image once your session is created.

Now let’s verify whether this session is added to the redis cache. Open the SSH terminal and type the following command to open redis cli.


On the command line, type in the following command to check whether a new key has been added:

keys *

If you find an alphanumeric key in mix, that key is the session key. This indicates that the Redis session is working correctly.

Redis As a Session Handler With Yii2

Let’s keep this console window open to check the cache key later.

Step 4: Using Redis As Cache Handler With Yii2

Now open the controller that was created earlier and add this new function in it.

   public function actionCaches()


       $cache = Yii::$app->cache;

       $key   = 'new';

       $data  = $cache->get($key);

       if ($data === false) {

           $key  = 'new';

           $data = 'A newly cache added';

           $cache->set($key, $data);


       echo $data;


Let’s run this action in the browser. You will get “A newly cache added” as an output. Let’s check whether it is saved in Redis or not. Switch over to the console which was left open and type the following command:

keys *

You will find your new cache key along with the session key that was generated earlier.

Using Redis As Cache Handler With Yii2


As you can see that it is easy to use Redis with Yii2. you just install the extension and connect Yii2 with it. You can then use the extension as either a session handler and cache for your app. Here is the complete list of topics covered in this series on Yii2. If you need to ask a question about this article, please leave a comment.

Share your opinion in the comment section. COMMENT NOW

Share This Article


Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!


Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour


  • 0


  • 0


  • 0


  • 0



For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now