Create a master Model and Controller in Yii2

In this article I discuss how to create a master Model and Controller using the Yii2 framework. Why would you want to do this? A master Model and Controller provides a place to put higher level methods which contain functionality that you want to use in your Models and Controllers. Rather than repeating code across multiple Models and Controllers put the code in the master Model and / or Controller and access it from the standard Models and Controllers. You can also override standard Yii2 Model and Controller functionality with your own altering the fundamental way Yii2 works without having to alter the core Yii2 files.

The master Model and Controller are classes that extend from the following …

  1. \yii\db\ActiveRecord – for Models
  2. \yii\web\Controller – for Controllers

Yii2 also contains another type of Model, this Model is used for defining data with no database table. This kind of model is useful for forms which do not need to use a database table but still need the inherit functionality within Yii2 Models like Validations. Although I will not be extending this second type of Model in this article you can use what you learn from this article to extend it yourself if you wish. The class for this Model is located at \yii\base\Model.

The diagram below shows what we are trying to accomplish …

Master Model and Controller

The “From” part of the diagram shows what the Yii2 software application without a master Model and Controller. The “To” part of the diagram shows the addition of the Master and Controller. Notice how the original ActiveRecord is called “ActiveRecord” and so is our master Model. Also the original Controller is called “Controller” and so is our master Controller. You do not need to worry about this as they exist in different Namespaces. You can call the master Model and / or Controller something different if you prefer.

Now lets look at the code to add the master Model and Controller to a Yii2 application. Create 2 new files one named “ActiveRecord.php” and one named “Controller.php” and place them inside the “components” folder inside your Yii2 application.

Now inside the files write the following code …

ActiveRecord.php

Controller.php

Now when it comes to creating your standard Models and Controllers you can extend from the master Model and Controller like follows …

Book.php (Model) …

BookController.php (Controller) …

The above 2 files would reside in the standard “models” and “controllers” folders.

Now any methods you want to use across multiple Models and Controllers can be placed inside the master Model and Controller. These will be accessible from the lower level Models and Controllers exactly as they would be in any extended PHP class. Also as mentioned earlier you can override standard Yii2 Model and Controller methods in the master Models and Controllers thus allowing you to alter the way Yii2 works without having to alter the core Yii2 files.

Remember if you wish to override the constructor in the master Model or Controller to call the parent constructor inside the override constructor.

A master Model and Controller are PHP classes I create for all my Yii2 applications. They are infinitely useful for the already mentioned above reasons.

5647 Total Views 26 Views Today

9 thoughts on “Create a master Model and Controller in Yii2”

  1. I have included examples in the code snippets …

    ActiveRecord.php and Controller.php are the masters.

    Book.php and BookController.php are the standard Models and Controllers which are extended from the masters.

    Whatever methods you put in the masters are accessible from the extended Models and Controllers.

  2. Do you know anything about performance hit when adding extra these “layers” of inherited classes?

    1. I have used the above code in most of my projects and performance has not been effected at all.

      I have not actually run any tests to determine though but I would think performance will be effected in terms of milliseconds if anything at all.

  3. Great post, great idea to put master Model and Controller in components folder. I named mine BaseModel and BaseController so when I use them I know to which one I am referring to. Here is how your constructor method would look like:

    I am always using fully qualified names like you. Thank you.

  4. If you have problems use this constructor …

  5. Hi,

    Nice post.
    But a have a different scenario..
    A have lots of table which is created dynamically in run time as required. So I cannot generate the CRUD of those tables. So I want to create a generic models and controllers (at least model), so that I can validate the form accordingly. In that model I’ll pass the table name to fetch the all the attribute name.
    Please help me out.

    1. I would like to help but the solution you are looking for would be much too large to explain in a blog comment. The solution would require many areas of thought and is not something that can be explained here.

Leave a Reply

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