2009-12-31 42 views
2

我想爲即時創建的論壇使用良好的mvc命名約定。適合論壇的MVC結構

我不知道,我應該使用這樣的結構:

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) 

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) 

這是使用MVC,所以我想知道的是這個最佳實踐我的第一次。我應該在第一個示例中將每個「事物」(標記,線程,用戶...)分開嗎?還是應該使用第二個?此外,我應該在第一個例子中分開評論和帖子,所以他們將成爲他們自己的控制器/模型?

會很好,如果有人給我一些好的mvc模式的論壇。

+0

多數民衆贊成在醜陋,通常程序員工作一個月,爲一個論壇計劃一個mvc,你想我們發佈的東西,即使是錯誤的,是無法使用的。對我來說這個問題不能回答。 – streetparade 2009-12-31 00:38:22

+0

它永遠不會讓你放下工作時間給我完整的描述。我只是想討論這個問題通常這樣我不是辦法客思權..即時通訊MVC的初學者以及programmering – ajsie 2009-12-31 02:29:56

回答

4

在你已經發布的2,id說第一個結構是最好的。把你的系統看作是獨立的實體,以及它們之間的關係。舉一個簡單的例子

Thread 
Reply 
User 
Tag 

在這種情況下SOM適當協會會..

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 

一旦協會是由它的更清楚一點想所需的方法,如:

**User** 
Get Threads: Returns all threads created by this user 
Get Replies: Returns all replies created by this 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 

很明顯會有更多的方法,例如在用戶模型中,您可能想要通過一個id返回一個特定的線程,因此您還需要一個GetThread方法來傳遞一個id。

希望這能讓你思考一下!

還有一點是,在你的模型中,比如你的標籤模型,你有一個方法addTag,據我所知,你不會真的想在你的模型中使用這個方法,因爲它只能是由標籤調用。如果你不得不創建一個標籤來添加標籤,你會很困難。 Id將其移入控制器。

+0

這看起來很合邏輯 – streetparade 2009-12-31 00:39:33

+0

thx爲一個很好的描述。只是一個問題。如果我想要例如創建一個線程。是否最好通過$ threads_model-> create_thread($ user_id)或$ users_model-> create_thread來完成它? – ajsie 2009-12-31 02:37:14

+0

沒問題。 模型的創建,更新和刪除通常由控制器交付。您可以將create()方法放入控制器中,或者,如果您使用的是類,請將其作爲Thread類方法。然後在控制器中,調用Thread.create()並傳遞你需要的參數,如title,body,user_id。我認爲MVC的方式是模型定義數據對象,控制器使用/在這些對象上執行動作,而視圖顯示對象。希望能幫助到你。 – cast01 2009-12-31 10:46:55

2

你的第一個結構會更好,從一開始就分開它,你永遠不知道何時未來的功能將需要一些標籤索引json或什麼。

每個(大多數)控制器都有它的CRUD操作指標,查看,編輯,新建,保存等

PHP的自動加載功能可以採取的型號名稱,然後看看進入車型目錄的文件要求( )。

是單獨的評論和帖子,到不同的模型和控制器中,帖子控制器將有一個視圖/顯示操作來呈現可能具有action =「」形式的帖子,指向該操作的保存/創建操作評論控制器。

「正常」 MVC文件系統結構可能是:

/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 

這種結構主要是由這是非常有組織的Rails的結構而。

+0

控制器不享有 – streetparade 2009-12-31 00:35:33

+0

@streetparade,他們當然不你在說什麼? – 2009-12-31 00:40:42

+0

你的控制器有內容並且有一個主視圖 – streetparade 2009-12-31 00:43:12