Yii - Controller Extension or CController?

I need to create a class where we must have the same methods shared with all my controllers, or almost all, anyway.

So, I was looking for a controller extension, but nonetheless, I noticed that the Controller Class already extending another CController class.

I'm confused.

Should we create our own class and expand CController or extend Controller or USE Controller ?

+4
source share
3 answers

You can write your common / shared methods in the Controller , this class is created only for this purpose (this is your class, do whatever you want). no need to create another class.

+3
source

There is little difference between them; The controller is just a child of the CController class.

In test applications, it is used to provide additional variables to all controllers derived from the controller. For example, it is used to set $breadcrumbs , $layout and the $variables menu for the layout view file.

It is good practice to extend the YII base classes (starting with the "C" character) in your project - the base classes will not be affected by YII updates, and the code will be more decoupled.

+1
source

The controller is only the CController child class. In this class code, ralated $ breadcrumbs, $ layout and menu $ variables for the layout are defined. If you use CController instead of the controller, you are in SiteController .. you will get an error.

0
source

Source: https://habr.com/ru/post/1436590/


All Articles