Chat with us, powered by LiveChat

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.

Say hello to redesigned Cloudways, an empowering Startup Program, enhanced Staging, a new Let’s Encrypt Wildcard SSL certificate feature, and more. GET STARTED

How to Setup Yii 2 With Redis Configuration

Updated on July 26, 2016

3 Min Read
Reading Time: 3 minutes

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:

Installing Redis Extension of Yii 2

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

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.

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.

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:

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.

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:

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

Ahmed Khan

Ahmed was a PHP community expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works. You can email him at

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

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