我想为我正在创建的论坛使用一个好的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 :(得分: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。
希望这能让你思考一下!
还有一点是,在你的模型中,比如你的Tag模型,你有一个方法addTag,据我所知,你不会真的想在你的模型中使用这个方法,因为它只能被调用通过标签。如果你必须创建一个标签来添加一个标签,你就会陷入困境。我将其移动到控制器中。
答案 1 :(得分:2)
你的第一个结构会更好,从一开始就将它全部分开,你永远不知道未来的功能什么时候需要一些标签索引json或其他东西。
每个(大多数)控制器都有它的CRUD动作索引,查看,编辑,新建,保存等
php自动加载功能可以获取模型名称,然后查看models目录中的文件require()。
是单独的评论和帖子,进入不同的模型和控制器,帖子控制器将有一个视图/显示操作来呈现可能有一个表单的帖子,其中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结构。