简体   繁体   中英

If else operation for pages yii2

I have 3 pages that is similar, Page A, Page B, and Page C. All of these pages are to create new data.

How to do if else using pages?

The ideas is like this:

Page A ----> actionCreate() -----> render to 'create'
Page B ----> actionCreate() -----> render to 'create-obituary'
Page C ----> actionCreate() -----> render to 'create-anniversary'

Controller.php

public function actionCreate()
{
    $session = Yii::$app->session;
    $model = new Memoriam(['scenario'=>'create']);

    if ($model->load(Yii::$app->request->post())) {
        //$model->owner_id = $session['UserID'];
        $model->picture1 = UploadedFile::getInstance($model, 'picture1');
        //$model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');
            if ($model->validate()) {
                if($model->picture1 != null)
                {
                    $dir = "img/".$session['UserID']."/memoriam/";

                    if(!file_exists($dir)) 
                    {
                        $old = umask(0);
                        mkdir($dir, 0777, true);
                        umask($old);
                    }
                    $path = md5(date("YmdHis")) . '.' . $model->picture1->extension;
                    $model->picture1->saveAs($dir.$path);
                    $model->picture1 = $path;
                    $model->page = 1;
                }



            if($model->save(false)){
                return $this->redirect(['site/profile', 'id' => $model->ID]);
            }
        }
        else {
            return $this->render('create', ['model' => $model]);
        }
    }

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

public function actionCreateDeparted()
{
    $session = Yii::$app->session;
    $model = new Memoriam(['scenario'=>'create']);

    if ($model->load(Yii::$app->request->post())) {
        //$model->owner_id = $session['UserID'];
        $model->picture1 = UploadedFile::getInstance($model, 'picture1');
        //$model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');
            if ($model->validate()) {
                if($model->picture1 != null)
                {
                    $dir = "img/".$session['UserID']."/memoriam/";

                    if(!file_exists($dir)) 
                    {
                        $old = umask(0);
                        mkdir($dir, 0777, true);
                        umask($old);
                    }
                    $path = md5(date("YmdHis")) . '.' . $model->picture1->extension;
                    $model->picture1->saveAs($dir.$path);
                    $model->picture1 = $path;
                    $model->page = 8;
                }



            if($model->save(false)){
                return $this->redirect(['site/profile', 'id' => $model->ID]);
            }
        }
        else {
            return $this->render('create-departed', ['model' => $model]);
        }
    }

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

public function actionCreateAnniversary()
{
    $session = Yii::$app->session;
    $model = new Memoriam(['scenario'=>'create']);
    $modelAnn = new Anniversary();

    if ($model->load(Yii::$app->request->post()) && $modelAnn->load(Yii::$app->request->post())) {
        //$model->owner_id = $session['UserID'];
        $model->picture1 = UploadedFile::getInstance($model, 'picture1');
        //$model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');
            if ($model->validate()) {
                if($model->picture1 != null)
                {
                    $dir = "img/".$session['UserID']."/memoriam/";

                    if(!file_exists($dir)) 
                    {
                        $old = umask(0);
                        mkdir($dir, 0777, true);
                        umask($old);
                    }
                    $path = md5(date("YmdHis")) . '.' . $model->picture1->extension;
                    $model->picture1->saveAs($dir.$path);
                    $model->picture1 = $path;

                }
            $model->page = 4;
            $model->save();
            $modelAnn->decease_id = $model->ID;
            $modelAnn->save();


            if($model->save(false)){
                return $this->redirect(['site/profile', 'id' => $model->ID]);
            }
        }
        else {
            return $this->render('create-departed', [
                'model' => $model,
                'anniversary' => $modelAnn]);

        }
    }

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

The quetion is that I want to use only one controller (which is actionCreate()) by all these 3 pages. It means i want to do if else for all these 3 pages(Page A,B and C).

I would suggest to pass some parameter to the action:

public function actionCreate($type = 'create')
{
    $typeToPageMapping = [
        'create' => 1,
        'departed' => 8,
        'anniversary' => 4,
    ]

    $model = new Memoriam(['scenario'=>'create']);
    if ($type == 'anniversary') {
        $modelAnn = new Anniversary();
    }

    if ($model->load(Yii::$app->request->post()) && ($type != 'anniversary' || $modelAnn->load(Yii::$app->request->post()))) {
        $model->picture1 = UploadedFile::getInstance($model, 'picture1');
        if ($model->validate()) {
            $model->picture1 = $this->savePicture($model);
            $model->page = $typeToPageMapping[$type];
            $model->save();
            if ($type == 'anniversary') {
                $modelAnn->decease_id = $model->ID;
                $modelAnn->save();
            }    
            if($model->save(false)){
                return $this->redirect(['site/profile', 'id' => $model->ID]);
            }
        }
    }

    return $this->render('create', [
        'model' => $model,
        'type' => $type,
        'anniversary' => $modelAnn ?? null,
    ]);
}

protected function savePicture($model)
{
    $session = Yii::$app->session;
    if($model->picture1 == null) {
        return null;
    }

    $dir = "img/".$session['UserID']."/memoriam/";

    if(!file_exists($dir)) 
    {
        $old = umask(0);
        mkdir($dir, 0777, true);
        umask($old);
    }
    $path = md5(date("YmdHis")) . '.' . $model->picture1->extension;
    $model->picture1->saveAs($dir.$path);
    return $path;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM