X
    Categories: Database Tutorials, Tips and GuidesLearn PHP Tutorials, Tips And GuidesYii Tutorials, Tips And Guides for beginners

How to Setup Yii 2 With Redis Configuration

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 as a session handler with custom PHP sites.

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"

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.

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.

<?php

namespace app\controllers;

use Yii;

use yii\filters\AccessControl;

use yii\web\Controller;

use yii\filters\VerbFilter;

class RedisController extends Controller

{

   public function actionIndex()

   {

       Yii::$app->session->setFlash('contactFormSubmitted');

       $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.

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.

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.

Summary:

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.

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 ahmed.khan@cloudways.com