Combine Dynamic Form and Complex Form

I have two different controllers. One serves the purpose of Dynamic Form while the other is for Complex Form.

Dynamic Form Controller




    public function actionCreate()

    {

        $model         = new Courses();

        $modelsCourseInstructors = [new CourseInstructors];


        if ($model->load(Yii::$app->request->post())) {


            $modelsCourseInstructors = Model::createMultiple(CourseInstructors::classname());

            Model::loadMultiple($modelsCourseInstructors, Yii::$app->request->post());




            // validate all models

            $valid = $model->validate();

            $valid = Model::validateMultiple($modelsCourseInstructors) && $valid;


            if ($valid) {

                $transaction = \Yii::$app->db->beginTransaction();

                try {

                    if ($flag = $model->save(false)) {

                        foreach ($modelsCourseInstructors as $modelCourseInstructors) {

                            $modelCourseInstructors->course_instructor_course_id = $model->course_id;

                            if (! ($flag = $modelCourseInstructors->save(false))) {

                                $transaction->rollBack();

                                break;

                            }

                        }

                    }

                    if ($flag) {

                        $transaction->commit();

                        return $this->redirect(['view', 'id' => $model->course_id]);

                    }

                } catch (Exception $e) {

                    $transaction->rollBack();

                }

            }

            // return $this->redirect(['view', 'id' => $model->id]);

        } else {

            return $this->render('create', [

                'model'         => $model,

                'modelsCourseInstructors' => (empty($modelsCourseInstructors)) ? [new CourseInstructors] : $modelsCourseInstructors

            ]);

        }

    }



Complex Form Controller




    public function actionCreate()

    {

        $model = new Courses();

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

        

    if ($model->load(Yii::$app->request->post()) && Yii::$app->request->isAjax) {

      //The course was created successfully, so we can use its data to make course structure

        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

                return ActiveForm::validate($model);

        }

        if ($model->load(Yii::$app->request->post())) {  

      

        $model->attributes = $_POST['Courses'];

        $model->course_start_date = date('Y-m-d', strtotime($_POST['Courses']['course_start_date']));

        $model->created_at = new \yii\db\Expression('NOW()');


        $coursefile = UploadedFile::getInstance($model, 'coursefile');

        if(!is_null($coursefile))

        {


     //   if ($this->validate()) { 

            foreach ($this->coursefile as $file) {

      //          $file->saveAs('uploads/' . $file->baseName . '.' . $file->extension);





          $model->course_file_name = $coursefile->name;

          $ext = end((explode(".", $coursefile->name)));

          // generate a unique file name to prevent duplicate filenames

          $model->course_file_path = Yii::$app->security->generateRandomString().".{$ext}";

          // the path to save file, you can set an uploadPath

          // in Yii::$app->params (as used in example below)

          //$Photo = Yii::getAlias('@web').'/../data/ins_images/no-photo.png';

          Yii::$app->params['uploadPath'] = Yii::$app->basePath . '/../web/data/course_admin_files/course_files/';

           // $model->course_file_path->saveAs(Yii::getAlias('@webroot').'/data/course_admin_files/course_files/'.$model->course_file_path = 'Course_'.date("d-m-Y_His").'.'.$model->course_file_path->extension);

          //$model->course_file_path->saveAs(Yii::$app->basePath.'/web/data/course_admin_files/course_files/'.$model->course_file_path = 'Course_'.date("d-m-Y_His").'.'.$model->course_file_path);

          $path = Yii::$app->params['uploadPath'] . $model->course_file_path;

          $file->saveAs($path);

        }//foreach


  //    }         //validate 

        }          

            $model->save(false);   

      

            if($model->save(false))

            //The course was created successfully, so we can use its data to make course structure

            {

 

              // check if topic format 

                if($model->course_format == Courses::TYPE_TOPIC) {      

                //loop through and make the course structure with topic#

                  // do your topic related stuff 

                for( $i = 1; $i <= $model->course_format_no; $i++ )

                {

                  $structure = new CourseStructure();

                    $structure->course_id = $model->course_id;                  

                    $structure->structure_name = $model->course_format . $i;

                    $structure->structure_id = $i;


                    // fill in other course structure data here

                    $structure->save();


                }

                }

                // check if weekly format 

                if($model->course_format == Courses::TYPE_WEEKLY)

                {

                  // do your weekly related stuff 

//                  var_dump('hello');

                  //var_dump ($model->getErrors()); die();

                }

                return $this->redirect(['index']);

            }                      

            else

                return $this->render('create', ['model' => $model,]);


                    

        } else {

            return $this->render('create', [

                'model' => $model,

            ]);

        }

    }



How do I combine the codes into One singular Controller