Showing posts with label yii controller createurl. Show all posts
Showing posts with label yii controller createurl. Show all posts

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,
            ]);
        }
    }
}

Recent Update

yii2 session handling

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

Most Search