Thursday, April 12, 2018

Yii Controllers

Create Controller and action in Yii2

Yii controller is a part of MVC. Yii controller define under the contollers directory.

Yii Controller extends the base class yii\web\Controller.

namespace app\controllers;

use Yii;
use app\models\User;
use yii\web\Controller;
use yii\web\NotFoundHttpException;

class UserController extends Controller
{
    public function actionUpdate($id)
    {
        $model = User::findOne($id);
        if ($model === null) {
            throw new NotFoundHttpException;
        }

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

    public function actionCreate()
    {
        $model = new User;

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }
}

1 comment:


  1. Wonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep Blogging!
    Yii Framework Development Company – Nintriva

    ReplyDelete

Recent Update

yii2 session handling

use yii\web\Session; $session = Yii::$app->session; // start session $session->open(); // close session $session->close(); ...

Most Search