How to load a component in console/shell

后端 未结 5 1547
耶瑟儿~
耶瑟儿~ 2020-12-20 17:22

In CakePHP, how do I load a component in a shell?

To use a component in a controller, you include an array of components in a property called $components. That doesn

5条回答
  •  时光说笑
    2020-12-20 18:18

    You simply don't.

    If you think you have to load a component in shell your application architecture is bad designed and should be refactored.

    Technically it is possible but it doesn't make sense and can have pretty nasty side effects. Components are not made to run outside of the scope of a request. A component is thought to run within the scope of a HTTP request and a controller - which is obviously not present in a shell.

    Putting things in the right place

    Why does XML manipulation stuff have to go into a component? This is simply the wrong place. This should go into a class, maybe App\Utility\XmlUtils for example and have no dependencies at all to the request nor controller.

    The logic is properly decoupled then and can be used in other places that need it. Also if you get incoming XML the right place to do this manipulation (by using your utility class) would be inside the model layer, not the controller.

    You want to learn about Separation of Concerns and tight coupling

    Because you've gone just against both principles.

    • https://en.wikipedia.org/wiki/Separation_of_concerns
    • What is the difference between loose coupling and tight coupling in the object oriented paradigm?

    Search before asking

    You could have tried to search via Google or on SO you would have found one of these:

    • using components in Cakephp 2+ Shell
    • CakePHP using Email component from Shell cronjob
    • Using a plugin component from shell class in cakephp 2.0.2
    • ...

    Be aware that some of them might encourage bad practice. I haven't checked them all.

提交回复
热议问题