Currently, the application uses the yaf framework. all controllers are inherited from the Base_controller. However, due to more and more features (such as permission management, product management, and log management ), as a result, Base_controller can no longer be bloated. of course, you can create different functions...
First
Currently, applications useyafFramework, all controllers areInheritanceFromBase_controllerBut due to the increasing number of features (permission management, product management, log management, etc.) in the later stageBase_controllerYou can no longer be bloated,
Of course, you can create different class files based on different functions, and thenBase_controllerBut such functions andBase_controllerStrong coupling, so I want to see if there is a better solution.
What I want now isDecoration mode, (Because this is the only option currently, mom and dad ),
Use specific decoration classes (permission management and log management) for decorationBase_controllerSo that it has these functions, but because the decoration mode requires that the decorator (Base_controller), And the specific modifier inherits from the same class, but nowBase_controllerIt has inherited from other classes, soBase_controllerCannot act as the decorator,
Who can I describe with the features (permission management, product management, and log management) that I have worked hard to write,
So I am wrong. the decoration mode is not suitable here, or do I need other designs?
Reply content:
First
Currently, applications useyafFramework, all controllers areInheritanceFromBase_controllerBut due to the increasing number of features (permission management, product management, log management, etc.) in the later stageBase_controllerYou can no longer be bloated,
Of course, you can create different class files based on different functions, and thenBase_controllerBut such functions andBase_controllerStrong coupling, so I want to see if there is a better solution.
What I want now isDecoration mode, (Because this is the only option currently, mom and dad ),
Use specific decoration classes (permission management and log management) for decorationBase_controllerSo that it has these functions, but because the decoration mode requires that the decorator (Base_controller), And the specific modifier inherits from the same class, but nowBase_controllerIt has inherited from other classes, soBase_controllerCannot act as the decorator,
Who can I describe with the features (permission management, product management, and log management) that I have worked hard to write,
So I am wrong. the decoration mode is not suitable here, or do I need other designs?
Use php trait
Thank you.
The Trait mentioned above is indeed a solution, but the key to the problem may not be here.
Your understanding of the decorative model is not accurate, but the problem is not big, and it is not the key.
In actual development, I have never encountered a very bloated problem with BaseController. this is usually the result of a developer's level (or realm). do not change the class code, this problem cannot be solved even if the class code is changed, but must be reconstructed.
Generally, BaseController is bloated because many methods should not be declared by the Controller. these methods may be declared in the Model or belong to Helper. this is the key issue. The Model is shared, so the method can be used in any Controller. If the method defined by the Model is put into the Controller, and the Controller is not public, the simplest solution is to put it in the Base, the long-term accumulation is what you see now.