Php 适合论坛的MVC结构
我想为我创建的论坛使用一个好的mvc命名约定 我想知道,我是否应该使用这种结构:Php 适合论坛的MVC结构,php,model-view-controller,Php,Model View Controller,我想为我创建的论坛使用一个好的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
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)
或
这是我第一次使用mvc,所以我想知道这方面的最佳实践是什么。在第一个示例中,我应该将每个“东西”(标记、线程、用户…)分开,还是应该使用第二个?此外,在第一个示例中,我是否应该将注释和帖子分开,这样它们就可以成为自己的控制器/模型
如果有人给我一些好的论坛mvc模式就好了。你的第一个结构会更好,从一开始就把它分开,你永远不知道将来的功能什么时候需要一些标签索引json或其他东西 每个(most)控制器都有其CRUD操作索引、查看、编辑、新建、保存等 php自动加载函数可以获取一个模型名,然后在models目录中查找所需的文件() 是将评论和帖子分开,放到不同的模型和控制器中,帖子控制器将有一个视图/显示操作来呈现帖子,该帖子可能有一个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
/app/
控制器/
content/在你发布的2个结构中,我认为第一个结构是最好的。将您的系统视为独立的实体,以及它们之间的关系。举个简单的例子
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返回特定的线程,因此您还将拥有一个传递id的GetThread方法
希望这能让你思考一下
还有一点是,在您的模型中,例如在您的标记模型中,您有一个addTag方法,据我所知,您不会真的希望在您的模型中使用此方法,因为这样它只能由标记调用。如果你必须创建一个标签来添加标签,你会被卡住。我把它放到控制器中。这是为了丑陋,通常程序员会花一个月的时间为一个论坛设计一个mvc,你希望我们发布一些甚至是错误的、无法使用的东西。对我来说,这个问题无法回答。你从来没有打算写下工作时间来给我完整的描述。我只是想笼统地讨论一下,这样我就不会偏离正确的思路。。我是mvc的初学者,也是programmering@streetparade,他们当然不知道你在说什么?你的控制器有内容,有主视图谁说他们输出?它们是脚本!查看OutputHX以获得更好的描述。只有一个问题。如果我想创建一个线程。最好是通过$threads\u model->create\u thread($user\u id)或$users\u model->create\u thread来完成吗?没问题。模型的创建、更新和删除通常由控制器进行。您可以将create()方法放在控制器中,或者,如果您使用的是类,则将其设置为线程类方法。然后在控制器中,只需调用Thread.create()并传递所需的参数,如title、body、user_id。我认为MVC的方式是模型定义数据对象,控制器对这些对象执行操作,视图显示对象。希望能有帮助。所以控制器将处理它。但是假设我有两个控制器(因为这是最好的解决方案),一个用户和一个线程,那么我应该用users/create_-thread或threads/create_-thread创建一个新线程吗?出于好奇,你使用的是什么编程语言?老实说,您可以使用这两种方法中的任何一种,我建议您将其保留在线程控制器中,因为它是您正在处理的线程,所以请使用Thread\u controller->create\u Thread。这样,如果以后你有另一个实体可以创建一个线程,你就不必再创建另一个方法了。那就用常识吧。我正在使用php:)这是一种非常容易学习的语言..但最难的部分是学习如何编写正确的结构:)
**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