Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cakephp Components VS Libs and shells

I'm working on a project and I'm confused as to put what code where. I'll put a simple example here as to show why I'm somewhat confused.

Imagine several webpages in a cakephp app where in one way or another an account can be created - together with this account, several other functions from both models and libs need to be called(example zip something, create a certificate for the account, ...).

Seeing as it needs to be done on several locations and doesn't quite belong in the Model(+is quite some code), the best way is to use components I thought.

This works great, as long as you're behind a web server, but what if I also want to have all that functionality available in a Shell? - then I can't use components, and can only load libs.

This is the case for all the functions/php code I can/want to share, to have all logic available in both the shells as in the controllers.

Here come the questions :) => So all my components need to be libs?, is it architectural correct to load models/other libs etc inside those libs?, what is the use of components then?, might as well 'rm -rf' them and only use libs?

Till now the way I've used components, is to put all the process logic that can/needs to be shared between controllers in them. Libs I've used when I don't need any Models or just a collection of static functions(example to manipulate images). Doing that I've hit a wall where I want/need to have access to the logic in those components when using Shell scripts.

Any idea's on where my understanding goes wrong?

like image 345
Nico Avatar asked Oct 04 '22 22:10

Nico


1 Answers

You have already given all the answers yourself!

Yes, components are there to share functionality between controllers. Only controllers. So if you need that functionality in a model or shell, than its misplaced.

You should put code, that is used both my shell and controller into the model layer (if its model/db related) or libs (static/calc stuff etc). Then those can be used from both sides.

You can use models in libs and vica versa. Just be careful not to create too many dependencies and cyclic relations. Those are also hard to test/extend/modify then.

Components are useful to wrap those lib/model methods for a quick and convenient controller access and also to do some more automagic and controller specific handling. Similar to the SessionComponent which only wraps the CakeSession class to provide an easy session access in the controller.

By the way: the same convinience wrapping you can also do in the command line environment using Tasks. Those are nothing else then "components for shells".

like image 174
mark Avatar answered Oct 07 '22 21:10

mark