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,
]);
}
}
}
ReplyDeleteWonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep Blogging!
Yii Framework Development Company – Nintriva