【问题标题】:appropriate MVC structure for a forum适合论坛的 MVC 结构
【发布时间】:2010-12-31 07:02:12
【问题描述】:

我想为我创建的论坛使用一个好的 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,所以我想知道什么是最佳实践。我应该在第一个示例中将每个“事物”(标签、线程、用户...)分开还是应该使用第二个?此外,我是否应该在第一个示例中也将 cmets 和帖子分开,以便它们成为自己的控制器/模型?

如果有人给我一些用于论坛的好的 mvc 模式会很好。

【问题讨论】:

  • 那是丑陋的,通常程序员工作一个月来为论坛计划一个 mvc,而您希望我们发布一些甚至是错误且无法使用的东西。对我来说这个问题无法回答。
  • 你从来没有打算放下工作时间来给我完整的描述。我只是想一般性地讨论它,所以我没有正确的想法。我是 mvc 和编程的初学者

标签: php model-view-controller


【解决方案1】:

在您发布的 2 个中,id 说第一个结构是最好的。将您的系统视为独立的实体,以及它们之间的关系。举个简单的例子

Thread
Reply
User
Tag

在这种情况下,适当的关联将是..

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。

希望这能让你思考一下!

还有一点是,在你的模型中,比如你的 Tag 模型,你有一个 addTag 方法,据我所知,你不会真的希望在你的模型中使用这个方法,因为它只能被调用通过一个标签。如果你必须创建一个标签来添加一个标签,你会被卡住。 Id 将其移动到控制器中。

【讨论】:

  • 感谢您的精彩描述。只有一个问题。如果我想创建一个线程。最好通过 $threads_model->create_thread($user_id) 还是 $users_model->create_thread 来完成?
  • 没问题。模型的创建、更新和删除通常由控制器处理。您可以将 create() 方法放在控制器中,或者,如果您正在使用类,则将其设为 Thread 类方法。然后在控制器中,只需调用 Thread.create() 并传递您需要的参数,例如 title、body、user_id。我对 MVC 的看法是模型定义数据对象,控制器对这些对象执行操作,视图显示对象。希望对您有所帮助。
  • 好的,我c。所以控制器将处理它。但是假设我有 2 个控制器(因为这是最好的解决方案)一个用户和一个线程,然后我应该使用 users/create_thread 或 threads/create_thread 创建一个新线程吗?
  • 出于好奇,您使用的是什么编程语言?老实说,您可以使用其中任何一种方法,id 建议将其保留在 Thread 控制器中,因为它是您正在处理的线程,因此有 Thread_controller->create_thread。这样,如果以后你有另一个实体可以创建一个线程,你就不必再做另一个方法了。
  • 好的,那我知道了。那只是使用常识。我正在使用 php :) 一种非常容易学习的语言..但最难的部分是学习如何编写正确的结构:)
【解决方案2】:

你的第一个结构会更好,从一开始就把它分开,你永远不知道未来的功能什么时候需要一些标签索引 json 什么的。

每个(大多数)控制器都有它的 CRUD 操作索引、查看、编辑、新建、保存等

php 自动加载函数可以获取模型名称,然后在模型目录中查找要 require() 的文件。

是的,将 cmets 和帖子分开,放到不同的模型和控制器中,帖子控制器将有一个查看/显示操作来呈现帖子,该帖子可能有一个带有 action="" 指向 cmets 的保存/创建操作的表单控制器。

“正常”的 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 结构,非常有条理。

【讨论】:

  • @streetparade,他们当然不知道你在说什么?
  • 你的控制器有内容并且有一个主视图
  • 谁说他们输出了?它们是脚本!查看输出
猜你喜欢
  • 1970-01-01
  • 2011-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-09
  • 1970-01-01
  • 2011-03-15
  • 1970-01-01
相关资源
最近更新 更多