简体   繁体   English

鉴于使用php逻辑(小剂量),对于MVC概念是否合理?

[英]Using php logic (at little dose) in view, is justified for MVC concept?

I am read about MVC pattern, that whole php logic must be in model, but when we have situation like this: from model passed mysqli_result object to view and in view we need check value from some column (mysql table column) and print something according to this value, that is in view we have: mysqli_result过MVC模式,整个php逻辑必须在模型中,但是当我们遇到这样的情况时:从模型传递mysqli_result对象进行查看,并且在视图中我们需要从某个列(mysql table列)中检查值并根据这个值,这是考虑到我们有:

while ($row = $my_mysqli_result_object->fetch_assoc()) {
    if ($row['some_column'] == "1") {
        // print something
    }
    else if ($row['some_column'] == "2") {
        // print something other
    }
    else if ($row['some_column'] == "3") {
        // print something other
    }
    // ....
    // ....
}

As you see, in view is php logic (few logic , but exists). 如您所见,在视图中是php逻辑(很少逻辑,但是存在)。

My question is: this dose of php logic in view, normally for MVC concept ? 我的问题是:鉴于这种逻辑,通常用于MVC概念? or this is not right way for MVC and must do something other, in such situations? 还是这不是MVC的正确方法,在这种情况下还必须做其他事情?

Some general info about MVC 有关MVC的一些常规信息

Each part of MVC design pattern has specific roles. MVC设计模式的每个部分都有特定的角色。 It is done by introducing two kinds of separations (concept introduced by Separation of Concerns principle): 它是通过引入两种分离(通过关注分离原理引入的概念)完成的:

  • Model layer [1] and Presentation layer within the system isolates domain business rules from visual representation 系统中的模型层 [1]表示层将域业务规则与视觉表示隔离开
  • Controllers and Views inside presentation layer makes sure that UI generation and user input are unattached to each other 表示层中的Controllers and Views确保UI生成和用户输入彼此无关

Actually, when you look at properly designed model layer , it also amalgamation of different structures for different tasks: application logic, domain logic or storage logic. 实际上,当您查看正确设计的模型层时 ,它还会合并用于不同任务的不同结构:应用程序逻辑,域逻辑或存储逻辑。

What are the Views? 有什么看法?

There is a common misconception, mostly perpetuated by Rails [2] fans and frameworks, which try to emulate it. 有一个常见的误解,主要是由Rails [2]的粉丝和框架(试图模仿它)造成的。 View is not a dumb template . 视图不是一个愚蠢的模板

Within modern interpretation of MVC, when applied for web, the Views are instances, which contain most of the UI logic. 在MVC的现代解释中,当应用于Web时,视图是实例,其中包含大多数UI逻辑。 Yes, logic. 是的,逻辑。 View should acquire information from model layer and, based on the data it got, decide what response to produce and which templates does it need to assemble that response. View应该从模型层获取信息,并根据其获取的数据来决定要生成的响应以及组装该响应所需的模板。

The how for acquisition of data is one of the primary factors that separated MVC-inspired patterns like Model2 MVC, MVVM and MVP. how用于采集数据的是,分离的MVC启发图案像Model2的MVC,MVVM和MVP的主要因素之一。

..now actually about the original question: ..现在实际上是关于原始问题的:

The code. 编码。 that you showed should be split, because it has two distinct goals: information retrieval from storage and making decisions about the output. 您显示的内容应该分开,因为它有两个不同的目标:从存储中检索信息以及对输出做出决策。 The fetch_assoc() bit should go into Model layer (most likely, within some data mapper instance. And your switch( $someColumn ) statement should be move to the view instance. fetch_assoc()位应进入“ 模型”层 (最有可能在某些数据映射器实例中。),并将switch( $someColumn )语句移至视图实例。

As a bit unrelated note, the example made me think, that you are actually dealing there with some status column there. 一点无关紧要的是,该示例使我想到,您实际上正在那里处理一些status列。 In that case it is a good idea to drop the if/else with and just use $status = $listOfOption[ $someColumn ] , where $listOfOptions = [0 => 'error', 1 => 'pending', 2 => 'unconfirmed', 3 => 'complete']; 在这种情况下,最好将if/else删除并使用$status = $listOfOption[ $someColumn ] ,其中$listOfOptions = [0 => 'error', 1 => 'pending', 2 => 'unconfirmed', 3 => 'complete']; ... or something along the lines ...或类似的东西

Your view should be one deciding how to represent that column and what value must be bound to templates. 您的视图应该是一种决定如何表示该列以及必须将哪些值绑定到模板的视图。

In MVC pattern all the logic is in the controller, not in the model. 在MVC模式中,所有逻辑都在控制器中,而不是模型中。

The model usually carry only the data. 该模型通常仅携带数据。 It could verify data correctness (that's the only elaboration delegated to the model). 它可以验证数据的正确性(这是委派给模型的唯一细节)。

The view can do few elaboration - ie it can have a little bit of logic inside - but only if it is related to the presentation aspect. 视图几乎不需要做任何细化-即它内部可以包含一点逻辑-但前提是它与表示方面有关。

Answering to your question: this dose of php logic in view is absolutely normal and acceptable. 回答您的问题:鉴于此,PHP逻辑绝对是正常且可以接受的。

You could tuck that function into the PHP model class, and call it in the view with 1 line of code. 您可以将该函数放入PHP模型类中,并在视图中用1行代码调用它。

MyModelClass.php MyModelClass.php

class MyModelClass {

    public static function getData($someColumn)
        //Do Query and get result
        $data_array = null;
        while ($row = $my_mysqli_result_object->fetch_assoc()) {
            if ($someColumn == "1") {
                    // print something
            }
            else if ($someColumn == "2") {
                // print something other
            }
            else if ($someColumn == "3") {
                // print something other
            }
            // ....
            // .... BUILD DATASET ARRAY
            // ....
         }
         return $data_array;
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM