表单处理逻辑在哪里属于MVC Web应用程序?

时间:2022-09-03 11:23:29

In a web-based application that uses the Model-View-Controller design pattern, the logic relating to processing form submissions seems to belong somewhere in between the Model layer and the Controller layer. This is especially true in the case of a complex form (i.e. where form processing goes well beyond simple CRUD operations).

在使用模型 - 视图 - 控制器设计模式的基于Web的应用程序中,与处理表单提交相关的逻辑似乎属于模型层和控制器层之间的某处。在复杂形式的情况下(即,表单处理远远超出简单的CRUD操作),尤其如此。

What's the best way to conceptualize this? Are forms simply a kind of glue between models and controllers? Or does form logic belong squarely in the M or C camp?

概念化这个的最佳方法是什么?表格只是模型和控制器之间的一种粘合剂吗?或者形式逻辑是否完全属于M或C阵营?

EDIT: I understand the basic flow of information in an MVC application (see chills42's answer for a summary). My question is where the form processing logic belongs - in the controller, in the model, or somewhere else?

编辑:我理解MVC应用程序中的基本信息流(请参阅chills42的摘要答案)。我的问题是表单处理逻辑属于哪里 - 在控制器中,在模型中,还是在其他地方?

4 个解决方案

#1


4  

I'd say this should probably be seen as 2 separate actions...

我想这应该被看作是两个单独的行动......

  1. submitting the form (V -> C)
  2. 提交表格(V - > C)

  3. processing the submission (C -> M)
  4. 处理提交(C - > M)

Speaking in generics, I tend to think as each action as a message between the sections. The full series of messages would be something like this...

在泛型中,我倾向于将每个动作视为各部分之间的消息。完整系列的消息将是这样的......

  • Display Form (C -> V)
  • 显示形式(C - > V)

  • Submitted by the user (V -> C)
  • 用户提交(V - > C)

  • Process contents (C -> M)
  • 流程内容(C - > M)

  • Processing finished (M -> C)
  • 加工完成(M - > C)

  • Display Results (C -> V)
  • 显示结果(C - > V)

#2


2  

Agree with chills42, but would add to stuff as much down into the model as possible.
When a user submits (V->C) it is going to be submitted to some controller, and I argue it's best if the controller just acts as a dispatcher to decide what happens next, possibly based on some simple data point. Let the model have a method (usually not strictly ORM or active record based) process the raw data and save it into the db if appropriate then simply return a status or throw an error.

同意chills42,但会尽可能多地添加到模型中。当用户提交(V-> C)时,它将被提交给某个控制器,我认为最好是控制器只是作为调度员来决定接下来会发生什么,可能是基于一些简单的数据点。让模型有一个方法(通常不是严格的ORM或基于活动记录)处理原始数据并在适当时将其保存到数据库中然后只返回状态或抛出错误。

#3


2  

Although at first the idea of using V -> C, C -> M, M -> C looks good, any change to the form requires messing up with the controller+model+view. That should be avoided to keep the application logic simple. Here is a very simple extension to the framework that makes it really easy to handle web forms processing, by delegating the form processing logic to a separate class and keeping the MVC architecture to handle the application logic.

虽然起初使用V - > C,C - > M,M - > C的想法看起来不错,但是对形式的任何改变都需要弄乱控制器+模型+视图。应该避免这样做以保持应用程序逻辑简单。这是一个非常简单的框架扩展,通过将表单处理逻辑委托给一个单独的类并保持MVC架构来处理应用程序逻辑,使得处理Web表单处理变得非常容易。

For each form you need to process, create a class derived from a generic "webform" class, or the codeigniter model class. Add methods like validate(), process(), display() to this class.

对于需要处理的每个表单,创建一个派生自通用“webform”类或codeigniter模型类的类。将validate(),process(),display()等方法添加到此类。

In the controller, the code becomes like this.

在控制器中,代码变成这样。

class User_controller
{

    function login()
    {
        $form = new LoginForm(); // this is the class you would create
        if ($form->validate())
        {
            $data = $this->user_model->getUserData( $form->userid );
            // form processing complete, use the main "user" model to fetch userdata for display,
            // or redirect user to another page, update your session, anything you like
        } else {
            $form->display();
        }
    }
}

The display method in the form class loads its own view, and populates post back data as desired. By using the above, there are few advantages:

表单类中的display方法加载自己的视图,并根据需要填充回发数据。通过使用上述内容,几乎没有什么优势:

  • You don't need to change your main controller if the form display or processing needs changes.

    如果表单显示或处理需要更改,则无需更改主控制器。

  • You don't need to change your user model either

    您也不需要更改用户模型

  • Controller remains clean and handle the main page logic

    控制器保持清洁并处理主页面逻辑

  • User Model remains cleans and only interacts with database

    用户模型保持清除并且仅与数据库交互

The framework can itself be updated so that webforms can be loaded using

框架本身可以更新,以便可以使用加载webforms

$this->load->form("login"); ......

However, that is only a suggestion that is useful for the codeigniter team.

但是,这只是对codeigniter团队有用的建议。

#4


1  

Form processing should take place in the model, because that's the layer of the application that is receiving and processing data from controllers by the way of views. The controller moves it around, but as for the actual code execution, it should be happening in your models.

表单处理应该在模型中进行,因为这是通过视图方式从控制器接收和处理数据的应用程序层。控制器移动它,但是对于实际的代码执行,它应该在你的模型中发生。

#1


4  

I'd say this should probably be seen as 2 separate actions...

我想这应该被看作是两个单独的行动......

  1. submitting the form (V -> C)
  2. 提交表格(V - > C)

  3. processing the submission (C -> M)
  4. 处理提交(C - > M)

Speaking in generics, I tend to think as each action as a message between the sections. The full series of messages would be something like this...

在泛型中,我倾向于将每个动作视为各部分之间的消息。完整系列的消息将是这样的......

  • Display Form (C -> V)
  • 显示形式(C - > V)

  • Submitted by the user (V -> C)
  • 用户提交(V - > C)

  • Process contents (C -> M)
  • 流程内容(C - > M)

  • Processing finished (M -> C)
  • 加工完成(M - > C)

  • Display Results (C -> V)
  • 显示结果(C - > V)

#2


2  

Agree with chills42, but would add to stuff as much down into the model as possible.
When a user submits (V->C) it is going to be submitted to some controller, and I argue it's best if the controller just acts as a dispatcher to decide what happens next, possibly based on some simple data point. Let the model have a method (usually not strictly ORM or active record based) process the raw data and save it into the db if appropriate then simply return a status or throw an error.

同意chills42,但会尽可能多地添加到模型中。当用户提交(V-> C)时,它将被提交给某个控制器,我认为最好是控制器只是作为调度员来决定接下来会发生什么,可能是基于一些简单的数据点。让模型有一个方法(通常不是严格的ORM或基于活动记录)处理原始数据并在适当时将其保存到数据库中然后只返回状态或抛出错误。

#3


2  

Although at first the idea of using V -> C, C -> M, M -> C looks good, any change to the form requires messing up with the controller+model+view. That should be avoided to keep the application logic simple. Here is a very simple extension to the framework that makes it really easy to handle web forms processing, by delegating the form processing logic to a separate class and keeping the MVC architecture to handle the application logic.

虽然起初使用V - > C,C - > M,M - > C的想法看起来不错,但是对形式的任何改变都需要弄乱控制器+模型+视图。应该避免这样做以保持应用程序逻辑简单。这是一个非常简单的框架扩展,通过将表单处理逻辑委托给一个单独的类并保持MVC架构来处理应用程序逻辑,使得处理Web表单处理变得非常容易。

For each form you need to process, create a class derived from a generic "webform" class, or the codeigniter model class. Add methods like validate(), process(), display() to this class.

对于需要处理的每个表单,创建一个派生自通用“webform”类或codeigniter模型类的类。将validate(),process(),display()等方法添加到此类。

In the controller, the code becomes like this.

在控制器中,代码变成这样。

class User_controller
{

    function login()
    {
        $form = new LoginForm(); // this is the class you would create
        if ($form->validate())
        {
            $data = $this->user_model->getUserData( $form->userid );
            // form processing complete, use the main "user" model to fetch userdata for display,
            // or redirect user to another page, update your session, anything you like
        } else {
            $form->display();
        }
    }
}

The display method in the form class loads its own view, and populates post back data as desired. By using the above, there are few advantages:

表单类中的display方法加载自己的视图,并根据需要填充回发数据。通过使用上述内容,几乎没有什么优势:

  • You don't need to change your main controller if the form display or processing needs changes.

    如果表单显示或处理需要更改,则无需更改主控制器。

  • You don't need to change your user model either

    您也不需要更改用户模型

  • Controller remains clean and handle the main page logic

    控制器保持清洁并处理主页面逻辑

  • User Model remains cleans and only interacts with database

    用户模型保持清除并且仅与数据库交互

The framework can itself be updated so that webforms can be loaded using

框架本身可以更新,以便可以使用加载webforms

$this->load->form("login"); ......

However, that is only a suggestion that is useful for the codeigniter team.

但是,这只是对codeigniter团队有用的建议。

#4


1  

Form processing should take place in the model, because that's the layer of the application that is receiving and processing data from controllers by the way of views. The controller moves it around, but as for the actual code execution, it should be happening in your models.

表单处理应该在模型中进行,因为这是通过视图方式从控制器接收和处理数据的应用程序层。控制器移动它,但是对于实际的代码执行,它应该在你的模型中发生。