问题描述
我正在开发基于 php 的模型-视图-控制器结构化网站.我知道模型应该处理业务逻辑,视图向用户呈现 html(或其他),而控制器则促进了这一点.我遇到的问题是表格.我在控制器中放置了多少处理,我在模型中放置了多少?
i'm working on a php based model-view-controller structured website. i understand that the models should deal with business logic, views present html (or whatever) to the user, and the controllers facilitate this. where i'm running stuck is with forms. how much processing do i put in the controller, and how much do i put the my model?
假设我正在尝试更新用户的第一个 &姓.我想要做的是使用 ajax 向我的控制器之一提交表单.我希望数据(再次)在服务器端进行验证,如果有效则将其保存到数据库中,然后将 json 响应返回给视图,无论是成功还是错误.
assume that i'm trying to update a user's first & last name. what i want to do is submit a form using ajax to one of my controllers. i want the data to be validated (again) server side, and if valid save it to the database, and then return a json response back to the view, as either a success or error.
我应该在控制器中创建我的用户模型的实例,还是应该让控制器中继到模型中的静态方法?这里有两个例子来说明它是如何工作的:
should i create an instance of my user model in the controller, or should i just have the controller relay to a static method in my model? here is two examples of how this could work:
选项 #1:在模型中处理 post
exists) { // try to set these fields if( $user->set_firstname($_post['firstname']) and $user->set_lastname($_post['lastname']) ) { // no errors, save to the dabase $user->save_to_database(); // return success to view echo json_encode(array('success' => true)); } else { // error, data not valid! echo json_encode(array('success' => false)); } } else { // error, user not found! echo json_encode(array('success' => false)); } } } class user_controller extends controller { public function edit_user_form() { $view = new view('edit_user_form.php'); } public function edit_user_form_submit() { user::save_data_from_post(); } } ?>选项 #1:在模型中处理 post
exists) { // try to set these fields if( $user->set_firstname($_post['firstname']) and $user->set_lastname($_post['lastname']) ) { // no errors, save to the dabase $user->save_to_database(); // return success to view echo json_encode(array('success' => true)); } else { // error, data not valid! echo json_encode(array('success' => false)); } } else { // error, user not found! echo json_encode(array('success' => false)); } } } ?>这两个例子做了完全相同的事情,我意识到这一点.但是这样做有对错之分吗?我已经阅读了很多关于瘦控制器和胖模型的文章,其中选项 #1 来自哪里.你怎么处理这个?谢谢,很抱歉问这么长的问题!
the two examples do the exact same thing, i realize that. but is there a right and wrong way of doing this? i've read a lot about skinny controllers and fat models, where is where option #1 came from. how are you handling this? thanks, and sorry for the long question!
推荐答案
简而言之,您可以使用这两种方法中的任何一种 - 但您应该稍微改变它们.
put shortly, you can use either of these approaches - but you should change them a bit.
考虑一下:模型并不真正了解"post、get 和诸如此类的东西.他们应该只知道他们是什么业务相关的东西 - 在你的情况下是用户.
consider this: the models don't really "know" about post, get and whatnot. they should only know about whatever business-related thing they are - in your case a user.
因此,虽然可以使用方法 #1,但您应该不要直接从模型访问后期变量.相反,让函数采用一组参数,然后用于创建用户.
so while approach #1 can be used, you should not access post variables directly from the model. instead, make the function take an array of parameters which are then used to create the user.
通过这种方式,您可以轻松地重用代码,比如在 shell 脚本或其他任何地方,没有 $_post 之类的东西.
this way you can easily reuse the code, say in a shell script or whatever, where there is no such thing as $_post.
虽然第二种方法在控制器中更加冗长,但您也可以这样做.然而,也许更好的风格方法是使用服务类".该服务将有一个方法,比如说createuserfromarray",它接受一个数组并返回一个用户.同样,您可以将此方法作为参数传递给 $_post - 类似于您应该如何将它们传递给修改后的 #1 中的函数.
while the second approach is more verbose in the controller, it's something you could do too. however, perhaps a bit better approach in the style is to use a "service class". the service would have a method, let's say "createuserfromarray", which takes an array and returns a user. again, you would pass this method the $_post as parameters - similar to how you should pass them into the function in modified #1.
只有控制器应该直接处理输入.这是因为控制器处理请求,因此它可以知道 post.
only the controller should deal with inputs directly. this is because the controller handles the request, and thus it can know about post.
tl;dr 你的模型不应该直接使用像 $_post 这样的超全局变量.
tl;dr your models should never use superglobals like $_post directly.