There are 2 ways to make the dependent dropdown list in Yii framework 2.0.

  • Using plugin: http://demos.krajee.com/widget-details/depdrop
  • Original code

In this article, I show you how to do it with original php code.

Create Dropdown

First, You should know how to make dropdown in Yii2, this is the basic code which I copied from the documentation

echo $form->field($model, 'name_field')->dropDownList(
     [items],
     [options]
);

Model

I had 2 models

  • Category
  • Post

Views

use yiihelpersArrayHelper;
$dataCategory=ArrayHelper::map(commonmodelsCategory::find()->asArray()->all(), 'id', 'name');
      echo $form->field($model, 'category_id')->dropDownList($dataCategory,
           ['prompt'=>'-Choose a Category-',
           'onchange'=>'
                $.post( "'.Yii::$app->urlManager->createUrl('post/lists?id=').'"+$(this).val(), function( data ) {
                $( "select#title" ).html( data );
           });
      ']); 

      $dataPost=ArrayHelper::map(commonmodelsPost::find()->asArray()->all(), 'id', 'title');
      echo $form->field($model, 'title')
           ->dropDownList(
                $dataPost,
                ['id'=>'title']
           );

Yii2 has changed Chtml list to  ArrayHelper::map.

use yiihelpersArrayHelper;

Controllers

I created action List in Post Controller

public function actionLists($id) {
     $countPosts = commonmodelsPost::find()
          ->where(['category_id' => $id])
          ->count();
     $posts = commonmodelsPost::find()
          ->where(['category_id' => $id])
          ->orderBy('id DESC')
          ->all();
     if($countPosts>0) {
          foreach($posts as $post){
               echo "<option value='".$post->id."'>".$post->title."</option>";
          }
     }
     else{
          echo "<option>-</option>";
     }
}

References
http://www.yiiframework.com/wiki/723/creating-a-dependent-dropdown-from-scratch-in-yii2/