CakePHP, where to put the generic code?

I am developing an application in Cake that interacts strongly with the filesystem. The basic idea is to track and index a folder, its subfolders and files, and store metadata for each file in the database. It worked fine so far, but now I have a problem understanding the mechanics of MVC.

I got this heavy FileController with a lot of functions to check if a file is updated or moved, database records are updated, etc. Now I want to call some of these actions from a shell / cronjob and I want to call them in the browser too.

I've heard a lot of people complain about importing controllers in wrappers and I think I figured out why this is a bad idea. But now I want to use the same code that interacts with the FileModel directly in the wrapper and in the controller. Where to say? What is the best practice in this situation? In a component loading a model? In the controller, importing it into the shell?

Thanks for your help in advance <3


source to share

1 answer

I got this heavy FileController with lots of functions to check if the file is updated or if it is moved,

Wrong place, heavy controllers are definitely wrong. You want fat models, skinny controllers. If there is logic that fetches the metadata, I would probably put it in app / Utility / FileMetaData or app / Lib / FileMetaData.php and make it a new class. Depending on what it does, you can extend the main class Folder or File.

The metadata processing logic and folder reading should go into the model. A model can be used from the shell as in a controller by using the $ uses property with an array of models.

To initialize this class, I would use a helper method (I don't mean a view helper on this one!) On a model like getMetaDataReader () that returns an instance. The reason for this is the ability to mock the result of this method call in the unit test. Also, if you ever change the arguments of a class or constructor, you only have to change one place.

Controllers are clearly buggy in wrappers. Please do not apply. Of course, you can technically do the trick and create them in there, but that's just wrong and doesn't make any sense. If you think you need or "need" to do this, there is something wrong with your application architecture.

You might want to look at my FileStorage plugin . You can implement an event listener there, and when you save the file, the listener will automatically handle the metadata.



All Articles