MVC - 控制器/模型中的功能

发布于 2024-10-31 13:43:07 字数 1280 浏览 1 评论 0原文

我正在尝试使用面向对象设计来改进一些 PHP 脚本(现在它是带有一些面向对象部分的程序)。需要明确的是,我并不是要构建完整的 MVC 应用程序,而是要尽可能地将各个部分分开。我以前从未在 PHP 中使用过 MVC(只在 Java 中使用过一点)。

当使用 Google 时,我发现了 100 种不同的 PHP MVC 方法,但我找不到关于这个主题的好书。如果有人能给我推荐一本关于 PHP 面向对象设计的好书,我将不胜感激。

目前,将用户添加到数据库的部分(假设用户现在只包含名字)看起来像这样(users.php):

$validator = new UserValidator();

if ($validator->validate($_POST['user_firstname']))
  $result = $db->execute("INSERT INTO `users` (`user_firstname`) VALUES (?)", $_POST['user_firstname']);

知道添加用户可以在多个地方完成,我不希望代码重复,我将创建一个用户模型。该类将包含一个方法 addUser()。我有点困惑的是验证。 UserValidator 将检查所有字段是否填写正确。

我可以这样做:

$validator = new UserValidator();

if ($validator->validate($_POST['user_firstname']))
  $result = $user->addUser($_POST['user_firstname']);

但我也可以这样做:

$result = $user->adduser($_POST['user_firstname'];

现在 User 类将包含验证器,并且 addUser() 方法将执行此验证。假设上面的代码是控制器,什么选项是最好的?将验证功能委托给模型还是在控制器中执行?

当获取某个用户的信息时也会出现同样的问题。不是每个人都可以获得此信息,因此我的代码可能如下所示:

if ($user->hasAccess($_SESSION['id'], $_GET['id'])
  $user->getUserById($_GET'id']);

(hasAccess() 方法将检查登录的用户是否可以查看某个用户 ID 的详细信息)

但我也可以只调用 getUserById() 和检查您是否有权访问该方法。哪个选项是最好的?

谢谢你!

I'm trying to improve some PHP scripts using a OO design (it's procedural now with some OO parts). Just to be clear, I'm not trying to build a full MVC application, but I am trying to seperate parts as much as possible. I've never used MVC before in PHP (only a little bit in Java).

When using Google, I find 100 different MVC approaches for PHP and I can't find a good book on this subject. If anyone could suggest me a good book on OO design in PHP, it would be much appreciated.

Currently, a part to add a user to the database (assuming a user only contains a firstname for now), looks like this (users.php):

$validator = new UserValidator();

if ($validator->validate($_POST['user_firstname']))
  $result = $db->execute("INSERT INTO `users` (`user_firstname`) VALUES (?)", $_POST['user_firstname']);

Knowing that adding users may be done at multiple places and I don't want code repeat, I will create a usermodel. This class will contain a method addUser(). The thing I'm a bit stuck with is the validation. The UserValidator will check if all fields are filled in correctly.

I could do this:

$validator = new UserValidator();

if ($validator->validate($_POST['user_firstname']))
  $result = $user->addUser($_POST['user_firstname']);

But I could also do this:

$result = $user->adduser($_POST['user_firstname'];

Now the User-class will contain the validator and the addUser()-method will perform this validation. Assuming the above code is the controller, what option is the best? Delegating the validation functionality to the model or doing it in the controller?

The same problem applies when getting information of a certain user. Not everyone may get this info, so my code could look like this:

if ($user->hasAccess($_SESSION['id'], $_GET['id'])
  $user->getUserById($_GET'id']);

(The hasAccess()-method will check if the user who is logged in can view details of a certain user id)

But I could also just call getUserById() and check if you have access in that method. Which option is the best?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二智少女 2024-11-07 13:43:07

在您的第一个示例中,将验证逻辑放入调用 $result = $user->adduser($_POST['user_firstname']; 中是更简洁的方法。保持控制器精简并让理想情况下,您的控制器会协调将传递到您的“视图”的数据,无论

您的第二个示例是否有逻辑。 ) 不属于 getUserById() ,或者您可能为 getUserById() 创建了比该方法有意义的更多工作。尽可能保持类似的功能,但有些假设不能仅通过查看您发布的两行来做出。

In your first example, putting the validation logic in the call $result = $user->adduser($_POST['user_firstname']; is the cleaner way to go. Keep your controllers thin and let your models handle as much of the logic as possible. Ideally, your controller is coordinating data that will be passed to your "view", whatever that may be.

Your second example is less clear. You might have logic in hasAccess() that doesn't belong in getUserById() or you might be creating more work for getUserById() than makes sense for the method. It's always best to keep similar functionality as close as possible, but there are some assumptions that can't be made just by looking at the two lines you posted.

卷耳 2024-11-07 13:43:07

我经常使用 CakePHP,这是一个 PHP 的 MCV 框架,在这种情况下,UserValidator 将被分解为一个称为“组件”的单独实体。控制器会在保存之前调用组件进行验证,如果全部通过,数据将被发送到模型进行保存。

我认为创建一个单独的验证器类来检查控制器中的数据可能是正确的方法。

I user CakePHP pretty frequently, an MCV framework for PHP, and in such a case, the UserValidator would be broken out into a separate entity called a "component." The controller would call the component to do the validation before the save and then, if all passes, the data would be sent to the model for a save.

I think creating a separate validator class that can check your data in the controller may be the way to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文