Yii2 – What you need to know – Part II (MVC)

MVC

MVC

In part 2 of this series I am going to look at the core MVC concepts of the Yii2 framework. While the basic premise of the MVC structure of the Yii2 framework remains the same as that of Yii1.1, there are some subtle differences to be seen. As a result, when we delve deeper into the constructs of the MVC structure, we find a plethora of changes that will make development on the Yii2 platform a more rewarding experience.

Models

In Yii, Models extend from the yii\base\Model class. Models are typically used to both hold data and define the validation rules for that data (aka, the business logic). In addition, the business logic greatly simplifies the generation of models from complex web forms by providing validation and error reporting.

Yii models have the following basic features:

  • Attribute declaration: a model defines what is considered an attribute.
  • Attribute labels: each attribute may be associated with a label for display purpose.
  • Massive attribute assignment: the ability to populate multiple model attributes in one step.
  • Scenario-based data validation.

The Model class is also the base class for more advanced models with additional functionality, such as Active Record. Changes within the Active Record warrants a post unto itself and will be tackled in detail in a later post.

Attributes and Attribute labels remain more or less same as in the previous iteration of the framework. Where changes are more prominent are in the scenario and validation handling of Models. Instead of the previous one function to handle both scenarios and validation, Yii2, splits these into two functions: rules() and scenarios(). rules() specify the actual validation for any attributes whereas, the scenarios() function specifies which attributes are safe to be assigned to the model. Consider the following example:

Here, the password field is only assigned to the model in the signup scenario. As a result, it’s validation is only applied under that scenario. We feel this fundamental change in the way Models handle attribute validation and assignment is a major step forward and will make designing and developing forms more flexible and easy.

Massive assignment has changed a bit, too. Instead of doing:

you can just do:

The key thing here is is that load() will automatically check for an appropriate index within $_POST, defined by the override-able $model->formName().

Views

Lots of minor changes are peppered throughout the View component of Yii2. The most obvious and immediately noticeable change is that the render() function now returns a value rather than outputting it. An example:

As you can see the rendered content can be echoed (As you will see in the controllers section, it will be more common to return the output). Moreover, for those that yearn for template engine support in views, there are now new official extensions that support smarty and twig.

Also, one major change in views is the use of the $this variable. In Yii1.1, $this was a context of the controller invoking the view. But in Yii2, $this refers to the yii\web\View component. This allows for some useful and interesting things including setting page title and meta, registering scripts, etc. right from the view. This allows for more intuitive placement of these codes. The object that called the view can still be accessed using the $this->context call.

Controllers

First some code:

As can be seen, Yii2 makes really good use of the namespace feature of PHP. Another thing worth noticing is that the output of an action is usually returned. Other than these, the basics of the Controller remains true to its original self.

Conclusion

As can be seen almost all the changes in the MVC components of Yii2 are for the better and we believe they will provide an enhanced experience for the developer. In the next part, we aim to delve deeper into the Active Record and DB handling part of the framework as it has undergone lots of changes and as such would require a complete post to cover all the changes.

Navigate to other parts of the series:

Part I, Part III, Part IV and Part V

Note: Post was moved here from my old blog…

Leave a Reply

Your email address will not be published. Required fields are marked *