Step by step to create yii2 extension

Dear All,

Can anyone guide me on how to create an extension in yii2? I want to port yii timeout-dialog extension ( to yii2.

I can already make this work but not as extension. First, I install Jui, as required by the timeout-dialog. The instruction to install Jui was found here (

Second, I create TimeoutDialogAsset.php in frontend/assets (I used kartik advanced layout), as follow



 * @link

 * @copyright Copyright (c) 2008 Yii Software LLC

 * @license


namespace frontend\assets;

use yii\web\AssetBundle;

class TimeoutDialogAsset extends AssetBundle {

    public $basePath = '@webroot';

    public $baseUrl = '@web/frontend/assets';

    public $css = [



    public $js = [



    public $depends = [





I add the timeout-dialog.css and timeout-dialog.js to the respective js and css folders inside frontend/assets.

Third, in the layouts/main.php, I added



        $options = [

            // Get timeout settings from session settings.

//            'timeout' => Yii::$app->getSession()->getTimeout(),

            // Uncomment to test.

            // Dialog should appear 20 sec after page load.

            'timeout' => 300,

            'keep_alive_url' => Yii::$app->urlManager->createUrl('/site/keepalive'),

            'logout_redirect_url' => Yii::$app->urlManager->createUrl('/user-management/auth/logout'),



        <?php $this->registerJs('$.timeoutDialog(' . yii\helpers\Json::encode($options) . ');', \yii\web\View::POS_READY, 'timeout'); ?>


It is working as expected. I can see the timeout-dialog and it works as expected. That’s why I want to make this as extension so that I do not need to copy-paste this code to my new project every time I made it. I am new to yii2 but the composer way to install is very simple and quick compare to yii 1.1.x extension installation.

Please help me on put this into an extension…



PS: ALthough, this is nice to have extension, somehow I got two issues with this extension,

  1. In yii 1.1.x,

'timeout' => Yii::app()->getSession()->getTimeout()

, will get me the correct value of the timeout, but in yii2, if I used this line,

'timeout' => Yii::$app->getSession()->getTimeout()

, I will get very short timeout value. Any idea?

  1. I want to enhance this extension, so that it will work across the tabs in a browser. Currently, this extension will have weird (unexpected) behaviour when we open several tabs. One tab inactivity can cause log out for all other tabs.