views:

126

answers:

3

I'm kind of new to the whole MVC concept, since I just recently started developing a web site using CakePHP framework. Therefore I turn to you asking for how to achieve the following in a best-practice-way.

I want to be able to place a number of pictures in a directory, which then is scanned for all filenames in it. These filenames should afterwards be passed to an arbitrary view, which then loops through all filenames making img tags of them.

It would be nice if this could be done in a general way, so that I could reuse the code for same task but with a different directory name.

I have already tried the following two approaches. Nevertheless, non of these felt like the best way to do it, for some reason.

  1. Create a model with $useTable=false.
  2. Create an ordinary class and import it as a vendor.

What is the best way to achieve what I describe above?

A: 

I've done the following before: create an e.g ImageManager class and pass it as an IImageManager to the constructor of your e.g ImageController instead of passing a repository. The ImageManager could then do all the work that you mentioned.

EDIT: I did that in .net mvc and don't have experience of CakePHP, but I guess there's a place where you can register the concrete implementation of IImageManager as ImageManager so the framework would know what to pass to the controller's constructor

David Archer
This indeed sounds like a nice idea. However, I'm not really sure how to implement this in CakePHP, since I'm just a cakebaker novice. Could someone give a hint if this is possible in CakePHP, and in that case how?
joelpet
A: 

I would create a helper for this purpose.

Edit: And for trawling through the filesystem use the Folder class from within your helper.

dhofstet
Aha, that's an interesting suggestion; I have always thought that this is to much logic for views (and helpers) to handle, but then that might not be the case. I will think this approach over, and probably implement it if no one opposes.
joelpet
I oppose. I don't think view helpers are the correct place for logic that is trawling around the file system
David Archer
To add to my comment, the helper could eg. potentially create the img tags but not get the list of filenames
David Archer
Okay, that confirms what I've been thinking about it -- thanks! But the question remains, where should the logic take place that scans the file system and serves file paths? And how should it be transported to the view?
joelpet
Wish I could help more. Still think my answer provides a decent place for that logic. Then the list of filenames can be transported to the view as a list or array of strings
David Archer
@David Archer: Yes, your approach is probably a very good one. The problem is that I can't really translate it into CakePHP. For example, I'm not sure how to (if even possible) handle constructor parameters for controllers. If anyone with some CakePHP experience could make this a little more cake specific it would be really great!
joelpet
I was looking at the cake website, could 'components' be a way of achieving this. haven't really looked into it but looks like it could be right. http://book.cakephp.org/view/62/Components
David Archer
Thanks again, I really appreciate your share in the matter! And yes, a component might be the way to go. It feels like it's on the right level for this kind of logic and does also meet my wishes for reusability.
joelpet
glad to help :-)
David Archer
Also have a look at the folder class (I modified my original answer accordingly).
dhofstet
+1  A: 

I'm thinking your original idea is the better one, use the Model to traverse/read/write the directory. Think of the File structure as your data source. You can pass the dirname to the model with $this->data and then let it use the File class to retrieve what you need. This would make it portable across Controllers (with loadModel())

Later on down the road if you move your image paths into a DB you only have to re-write the model to take that into account.

Darren Newton
I'm glad you liked my own idea. :) However, I have a small concern regarding it. What would i call such a model, to make it reusable for many purposes (e.g. personal_photos, sponsor_logos directories), without breaking the CakePHP conventions where model names are in singular? It's a good point that one about easy refactoring.
joelpet
I would think you would want something generic, like for instance 'image'. I have an Image model in an App I'm currently working on.
Darren Newton
That should make it! I'll try this out and mark your answer as accepted, if I don't encounter any problems. Thanks!
joelpet
Worked like a charm and feels pretty good too!
joelpet