Welcome Guest, Not a member yet? Register   Sign In
The correct way to use and structure controllers.. etc.?
#1

[eluser]rreynier[/eluser]
So I have recently discovered Codeigniter and im falling in love with it.

So anyway, i'm going to start my first test development site. It will be a simple site for the USF Ultimate Frisbee club.

Anyway, I want to have a news page, a gallery, a blog, some information pages, an event sign up page.

My question is.. how is it best to structure the controllers.

For example, if a person wants to visit the image gallery.. should there be a controller called gallery.. or should there be a main controller.. that has a function gallery in it? Possibly this function would call the gallery controller. Then at the end the main controller calls the site controller which does all the rendering.

I guess the problem with the above is that you would end up having xyz.com/index.php/main/gallery instead of just xyz.com/index.php/gallery etc.

I have read quite a few tutorials etc, but just would like to get comfortable with a valid way to structure my controllers and flow of the site.

Any suggestions would be greatly appreciated!
#2

[eluser]steelaz[/eluser]
I would definitively use gallery controller. You can also group controllers into folders.
#3

[eluser]jedd[/eluser]
Correct. Gallery becomes a controller, and the first segment in your URL as a consequence.

You don't do this so much for the URL format -- you can work around your '/main/gallery/' problem, above, using routes -- but simply because a controller should typically concern itself with a single resource (or 'thing', if you prefer) within your system. Plus you don't want it getting too huge, just for the sake of manageability and, maybe, performance.

You'd also have one or more models that this controller talks to. The way I would do it would be to have a controller called Gallery, and a model called Picture or Image or Photo (etc - basically a synonym) that manages all the data storage and retrieval aspects for your images. The data you're managing in this case is likely to be a hybrid of several database tables, and a hierarchy in your file system somewhere to actually store the pictures.

Note there are, broadly speaking, two other common approaches to this.

First, have a model per table in your database (and possibly one more dedicated to managing the file system aspects) and your Gallery controller deals with all the involved models.

Second, have a model, much as I describe above, but called Gallery_model. (Personally I think that's the worst, but some people seem to enjoy it.)
#4

[eluser]BrianDHall[/eluser]
One big question here is how you want to use the gallery, and its functions. The reason I bring this up is this: on the front page do you want to be able to do something like "show last 10 new gallery submissions", or "top 5 gallery submissions", etc?

Since it seems logical to me that you want to use gallery functions elsewhere. I think you'd go for something just a touch different than what has been mentioned so far.

First, lets say you will want to allow users to submit their own pictures, have sets of related pictures (like "See last weeks tournament pictures", etc), maybe even rate or tag pictures. For the "user_admin" sort of functions like this, you could easily use a special controller - and then probably use the same controller for admin_only functions like approving photos, removing, categorizing, etc.

Now the controller controls what you might call "changes" to the model - the view might have a form for new picture submission, and the controller handles that function and then the model comes in. Once you have the file uploaded you might call $this->Gallery->add_picture($path, $category) - and then the model handles organization, saving information about where pictures are, etc.

The model might have functions like add_picture, remove_picture, get_related_photos, get_photos_by_category, get_thumbnail, whatever you need. One bit of advice - I just like writing functions as I actually need them. Any time I code a bunch of functions ahead of time I end up rarely using any of them. I should think this would encompass one table in the database and some file system functions, but YMMV.

For views you just keep in mind some might want to be used in multiple places, but not a big worry. You could easily have a partial view that handles displaying a 'box' with rows of pictures from a certain category, etc.




Theme © iAndrew 2016 - Forum software by © MyBB