2009-12-31 41 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