Basic Understanding


I am digging in Yii2 and I have some questions, but I don’t find the explanations online.

On a basic model, whatever, say post.

  • the views/post/index.php, generated using Gii, has a ‘create button’.

  • when pressed, yii loads views/post/create.php

in create.php code we see that a param @var $model is existing. ($model is set to app\models\Post in the comment on top of create.php)

Question1) how does $model is being sent from index.php to create.php ?

  • then in create.php, yii renders _form.php

Question2) if we press the submit button, why does the default action is create() in PostController.php ?

there is no action specified in the ActiveForm::begin();

but when we press the submit button, a new record is saved using the public function actionCreate() in PostController.php (if I am not mistaking)

I am sorry if this is easy, it is not to me :slight_smile:

many thanks in advance for taking the time to explain this to me.

best regards



when you press on index.php the create button call the action "Create" in "app\Controllers\PostController"

the action render the $model in Create.php

   if( request post ...){


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




the create.php render the $model to _form.php

in _form call the action create or update.

in "app\Controllers\PostControlller" the rest will do the action Method.

diffrent between new create and update is the the $model(the update $model allready loaded with data)


   $model = $this->findModel(id); // here its load data from database



   $model = new Post(); // here its create new


hope it was understandable

  1. @var $model is just a comment that helps IDE to autocomplete code. Nothing more. It doesn’t set anything by itself. Model is coming from either controller or parent view via ->render() method.

Hi olivier,

Note that "create" action is called twice … one for rendering the form and the other for processing the post.

When you have clicked on “create” link of the index page, then your browser will request “create” action with get method without any parameter. And the “create” action will render the form with a model that is empty. “if” block is skipped because there’s no post parameters yet.

Then, when you have submitted the form, your browser will request "create" action with post method. Note that the action of an ActiveForm defaults to the current action. And the "create" action will now enter into "if" block and receive the post parameters to save the model.

"Update" action is at first called from the index page using get method with "id" parameter. So it will render the form with a model that has a content retrieved from the database, as sukran has just said. The rest of the procedure is quite similar to that of "create" action, so we can share the _form.php partial view for both the actions.

Hello Sam

I understood it was comment, but I wrote this because it I thought this meant that those things written in comment are the params received by this ‘file’.

I signed at github and stared Yii2 :slight_smile:

thank you for your time answering and helping me.


Hello Sukran,

I see my mistake at the beginning. hank you for explaining me this.

I follow you until :

"the create.php render the $model to _form.php".

After this, I don’t see how the actions ‘create’ or ‘update’ are triggered…

when you say "in _form call the action create or update." what part of the code does this exactly ?

the submit button has an IF statement to label the button ‘create’ or ‘update’ wether it is a new record (if I understand well). but ‘create’ and’update’ are only labels.

So when I press the submit button, what code acts to trigger the ‘create’ action ?

How does the form ‘know’ what action to take at submit ?

Thank you for your time answering and helping me.


Hello SoftArk,

here is my second answer :slight_smile:

"Note that the action of an ActiveForm defaults to the current action. "

many thanks to all of you !