tags:

views:

74

answers:

2

i want to use a good mvc naming convention for a forum im creating.

i wonder, should i use this structure:

controller: threads
model: threads_model (eg. $threads_model->get_all_threads, $threads_model->add_thread, $threads_model->add_post, $threads_model->add_comment)

controller: tags
model: tags_model (eg. $tags_model->get_all_tags, $tags_model->add_tag)

controller: users
model: users_model (eg. $users_model->get_all_users, $users_model->add_user)

or

controller: content
model: content_model (eg. $content_model->get_all_tags, $content_model->get_all threads...)

controller: users
model: users_model (eg. $users_model->get_all_users, $users_model->add_user)

this is my first time using mvc so i want to know what is best practice for this. should i like in the first example separate every "thing" (tags, threads, users...) or should i use the second one? further more, should i in the first example separate comments and posts as well so they will be their own controllers/models?

would be good if someone gave me some good mvc pattern for forums.

A: 

Your first structure would be better, separate it all from the start, you never know when a future feature will need some tags index json or something.

Each (most) controller has it's CRUD actions index, view, edit, new, save etc

The php auto load function can take a model name and then look into the models directory for a file to require().

Yes separate comments and posts, into different models and controllers, the post controller will have a view/show action to render the post that might have a form with an action="" pointing to the save/create action of the comments controller.

The "normal" MVC filesystem structure might be:

/app/
     controllers/
                 content/                <- controller name
                       home.php          <- actions that require(../../views/content/home.php)
                       view.php
                 users/
                       index.php
                       view.php
                 tags/
                       edit.php
     models/
                 content.php             <-   class Content{ }
                 user.php
                 tag.php
     helpers/
                 application.php       <- grouped up functions for areas of the system
                 tag_helper.php
                 content_helper.php
     views/                             <- templates
                 users/
                        index.php
                        user.php
                        views.php
     public/
                 css/
                        layout.css
                        typography.css
                        style.css
                 images/
                        logo.png
                 js/
                        global.js
                        content.js
                 assets/
                        users/
                                000234.png     <- e.g. profile images

This structure is largely taken from the Rails structure which is very organized.

Question Mark
controllers dont have views
streetparade
@streetparade, of course they don't what are you talking about?
Question Mark
your controller has content and has a home view
streetparade
Who said they output? they are scripts! views output
Question Mark
+2  A: 

Over the 2 you have posted, id say the 1st structure is best. Think about your system as separate entities, and the realations between them. As a simple example

Thread
Reply
User
Tag

In this case som appropriate associations would be..

User can create many threads
User can create many replies

Reply Belongs to a User
Reply Belongs to a thread

Thread belongs to a user
Thread has many replies
Thread has many tags

Tag has many threads

Once the associations are made its a bit clearer to think of the methods needed, such as:

**User**
Get Threads: Returns all threads created by thhis user
Get Replies: Returns all replies created by thgis user

**Thread**
Get User: Returns the User that created this thread
Get Replies: Return all replies to this thread

**Reply**
Get User: Returns the User that created this reply
Get Thread: Returns the Thread that this user belongs to

There would obviously be more methods for example in the user model you may want to return a particular thread by an id so you would also have a GetThread method that you would pass an id.

Hope this gets you thinking a bit!

One further point is that in your models, such as your Tag model, you have a mehtod addTag, as far as i am aware, you wouldnt really want this method in your model, as then it could only be called by a tag. If you had to create a tag to add a tag youd get pretty stuck. Id move this into the controller.

cast01
this looks logical
streetparade
thx for a great description. just one question. if i want eg create a thread. is it best to do it by $threads_model->create_thread($user_id) or $users_model->create_thread?
weng
No problem.Creation,Updating and Deletion of models is usually handed by the controller. You can either put the create() method inside the controller, or, if you are using classes, make it a Thread class method. Then in the controller, just call Thread.create() and pass the parameters you need such as title, body, user_id. The way i think of MVC is that Models define the data objects, Controllers perform actions with/on those objects, and Views display the objects. Hope it helps.
cast01
ok i c. so the controller is going to handle it. but lets say i've got 2 controllers (cause it was the best solution) one users and one threads, should i then create a new thread with users/create_thread or threads/create_thread?
weng
Out of curiousity, what programming language are you using?To be honest you can use either of those methods, id recommend keeping it within the Thread controller as its a thread you are dealing with so have Thread_controller->create_thread. That way, if later you have another entity that can create a thread, you dont have to make another method.
cast01
ok then i know. it is just to use common sense then. im using php:) a very easy to learn language..but the hardest part is to learn how to code the right structure:)
weng