我可以做些什么来改进我的 MVC?

发布于 2024-09-07 15:29:00 字数 283 浏览 6 评论 0原文

我正在考虑在深入研究之前重新设计我的 MVC。目前它使用单个控制器来接受用户输入并确定模型。该模型可能有不同的方法,我将其称为操作,因为其中一种方法是自动调用的。模型加载其视图文件,该文件又包含许多不同的方法。模型可以设置可在视图中使用的属性。然后控制器调用模板类来解析输出以进行显示。

这是最好的方法吗?

或者每个不同的部分(新闻、联系人、关于)应该有自己的控制器,用于加载特定的模型和视图。本质上,它不是将方法分组到单个文件中,而是使用多个文件。

我有点迷失了,不知道该怎么做。

干杯

I'm thinking of re-working my MVC before I get to far along with it. At the moment it uses a sinle controller which takes the user input and determines the model. The model has maby differ methods which I call actions, because one is called automatically. The model loads its view file, which again holds many different methods. The model can set properties which can be used in the view. Then the controller calls th template classwhich parses the output for display.

Is this the bst way to do it?

Or should each different part (news, contact, about) have its own controller, which loads a specific model and view. Essentially, instead of grouping methods into a single file, it uses multipe files.

I'm kind of lost as to how I should do it.

Cheers

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

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

发布评论

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

评论(4

风追烟花雨 2024-09-14 15:29:00

开始使用有效且众所周知的 MVC,例如 Symfony 或 Cake。由此您将决定:

  • 了解最佳实践,自己做什么;
  • 如果您觉得使用这些可以节省时间,请放弃自己的。

Start using a MVC that works and is well-known like in Symfony or Cake. From that you will decide:

  • what do to in your own, knowing the best practices;
  • to drop your own if you feel like you can save time by using theses.
暮光沉寂 2024-09-14 15:29:00

如果您正在考虑推进自己的 MVC 模型,就像 @e-satis 所说的那样,您将需要体验已经开发的系统中正在发生的情况。然而,根据我设计 MVC 模型和确定开源社区中的内容的经验,我坚持使用自己的 MVC 有两个很好的理由。一个原因是定制的灵活性,另一个原因是MVC本身的隐私性。

我对 MVC 设计模式使用了以下方法。

标识用户请求 URL 的 Router.php 文件。该路由器将能够获取控制器并包含文件并调用控制器默认方法。

如果需要运行,加载的控制器还可以加载其他控制器。这是使用全局方法完成的,其中所有控制器类将扩展为能够调用其他控制器的 MainController 类。

我确实使用全局注册表来设置和获取从一个控制器到另一个控制器的变量。

模型用于从表中获取数据,我的大多数模型将代表数据库功能,其中包括 CRUD(创建读取更新删除)。这样控制器就可以使用模型轻松地操作数据库表数据。

如果您希望系统更加智能,在知道文件名的情况下识别所需的操作,所有控制器、模型和视图中的命名约定也很重要。

我对每种类型的控制器分别使用视图。这些视图将被发送到主模板视图文件。
与模型相同,控制器将能够将视图设置为主视图。

您还可以进行其他自定义,例如在调用类之前或调用类/控制器/模型/视图等之后应用安全方法。
这是由 MainController 完成的,它始终会查找具有自动加载类的文件夹,该类说明在构建内容和交付输出的过程中,不同操作之前和之后应该加载哪些文件。

MVC不是一个小规模的想法,而是一个可以不断发展的设计理念。如果你知道如何搜索像 google.com 这样的主要搜索引擎,那么可以找到很多 PHP MVC 开源框架。

但是我建议你,如果你只是开发一个小型动态网站,MVC 并不是一个好的解决方案,因为与开发小型网站相比,它会花费更多的时间来开发。如果您有业务逻辑并且需要系统自动化以避免大多数常规开发任务,那么 MVC 是理想的选择,并且我想说 MVC 对于大型应用程序来说是最理想的。

If you are thinking of advancing your own MVC model, like @e-satis have said, you will need to experience what is happening in already developed systems. However, as based on my experience in designing MVC model and determining what is there in opensource community, I stick back to my own MVC for two good reasons. One reason is the flexibility of customization and the other is own MVC privacy.

I used the following approach for MVC design pattern.

A Router.php file identifying user-request urls. This router will be able to fetch controllers and include the file and call the controller default method.

The loaded controller is also able to load other controllers if required to function. This is done using a global method, where all controller Class will extend to a MainController Class which is able to call to other controllers.

I do use a global registry to set and get variables from one controller to the other.

The Models are used to get the Data from Table, and most of my Models will represent Database functions which includes CRUD (Create Read Update Delete). So that a controller can easily manipulate database table data using a model.

Naming conventions in all controller, models, and views is also important, if you want to system to be more intelligent to identify the required action knowing the file name.

I use the Views separately for each type of controller. And these views will be sent to a Master Template View file.
Same as models, the controller will be able to set Views to Master View.

There are other customizations which you can do, like applying security methods before calling a class, or after calling a class/controller/model/view etc.
This is done by the MainController, which it will always look into a folder with autoload class which states what files should be loaded before and after of different actions during the process of building the content and delivering the output.

MVC is not a small scale idea, but it is a design idea which can always be developed. There are so many PHP MVC open source frameworks to be found if you know how to search the major search engines like google.com

But I do advice you that, MVC is not a good solution if you are simply developing a small dynamic website, as it will consume more time in developing compared to developing small websites. MVC is ideal, if you have business logic and needs system automation in order to avoid most routine tasks of developing, and like that I would say MVC is most ideal for larger applications.

凡尘雨 2024-09-14 15:29:00

模型加载其视图文件

控制器应充当模型和视图之间的中介者。

模型不应该知道视图渲染它的方式,视图也不应该知道模型的任何类型的逻辑。

理论上,如果您的 MVC 结构良好,您应该能够使用不同类型的视图(例如 HTML、XML、JSON)表示相同的模型。

The model loads its view file

The Controller should act as a Mediator between the Model and the View.

The Model shouldn't be aware of the way the view renders it, and also the View shouldn't be aware of any kind of logic of the Model.

In theory, if your MVC is well structured, you should be able to represent the same Model with different types of Views (HTML, XML, JSON for example).

双手揣兜 2024-09-14 15:29:00

构建 FrontController,它解析请求 uri 并决定加载哪个控制器以及运行哪个方法。使用 .htaccess 重写所有请求到index.php

//index.php
class FrontController{
  function run(){
    //parse request uri here /comment/new
    //load controller  comment
    //run controllers method new and pass some request attributes
  }
}

// ../controllers/comment.php
class Controller_Comment extends Controller{
  function new($request){
    //do stuff here
  }
}

Build FrontController which parses request uri and decides which controller to load and which method to run. With .htaccess rewrite all request to index.php

//index.php
class FrontController{
  function run(){
    //parse request uri here /comment/new
    //load controller  comment
    //run controllers method new and pass some request attributes
  }
}

// ../controllers/comment.php
class Controller_Comment extends Controller{
  function new($request){
    //do stuff here
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文