use cakephp component inside a model

后端 未结 3 2156
长发绾君心
长发绾君心 2021-02-13 05:03

How do I use a component that I created in cakePHP inside one of my model classes? Is this possible?

If so, please let me know how I can do so

3条回答
  •  耶瑟儿~
    2021-02-13 05:47

    It is possible but pretty bad practice in a MVC framework. You should re-think and re-organize your code if you think you need to use the component in a model because something is cleary wrong then.

    A component is thought to share code between controllers, only between controllers.

    • Components in CakePHP 1.3
    • Components in CakePHP 2.x
    • Components in CakePHP 3.x

    To share re-usable code between models it would be a behavior. For a view it would be a helper.

    If you have some really generic code it should be a lib or put it in the Utility folder / namespace or create a new namespace. Check the existing classes there to get an idea what to put in there.

    No code was provided so it is not possible to give any real recommendation on how to refactor it. However the way you want to use the existing code won't work in the MVC context, so time to rethink your approach of whatever you try to do.

提交回复
热议问题