dongye9191 2014-03-08 15:36
浏览 38
已采纳

PHP MVC - 当标签字符串随对象类型变化时,标签字符串是否成为模型的一部分而不是视图?

I have a "summery" view for an Iterated list of polymorphic objects, and I experimenting with a clean separation of the view and the model.

So, where do I put the static label information? I am trying to avoid this sort of thing in the view...

while loop do
  switch (prop->type)
    case foo:
      echo "label1: ". prop->data1;
      echo "label2: ". prop->data2;
    break;
    case bar:
      echo "label3: ". prop->data1;
      echo "label2: ". prop->data2;
      ...

This can make the view code pretty messy, especially when you have up to 10 different prop->type and the summery has to be shown in several different areas.

I guess the real question here is...has the label ceased to be part of the view and now is part of the model because it changes dynamically depending on the object type?

  • 写回答

1条回答 默认 最新

  • doushe7934 2014-03-08 16:36
    关注

    Using a switch statement isn't probably the best practice here. Everytime you need a new type you would have to add a new case. It would be better to store the corresponding labels along with the data in your class in my opinion. But let's tackle the problem.

    Decorator

    One thing you can do is to implement a Decorator for your prop class that adds a functionality to get the corresponding Label.

    class PrintLabelPropDecorator {
    
        protected $prop;
    
        public function __construct(Prop $prop)
        {
            $this->prop = $prop;
        }
    
        public function printLabels()
        {
            switch($this->prop->type)
            {
                case 'foo':
                    echo 'Label1: ' . $this->prop->data1;
                    echo 'Label2: ' . $this->prop->data2;
                break; 
    
                //[...]
            }
        }
    
    }
    

    The first thing we do is to inject the class we want to decorate into the constructor. Then we write the actual decoration method printLabels(). That does the actual check and prints the label information.

    Now we only need to decorate the actual class. For simplification reasons I do this in a pseudo-controller method. You should outsource it by your demands.

    <?php
    public function myControllerMethod()
    {
        //$props = whatever you need to do to get the prop objects
    
        $printLabelPropDecorators = [];
    
        // That should usually better not be handled by the controller
        foreach($props AS $prop)
        {
            $printLabelPropDecorators[] = new PrintLabelPropDecorator($prop);
        }
    
        $this->view->create('myfancyview', compact('printLabelPropDecorators'));
    
    }
    

    We create an array that will hold our decorated objects and then iterate trough the base objects and create our decorator objects. Then we simply pass the array with our decorators to the view. (The decoration process is not quite elegant, you could for example write some kind of Collection for this).

    Now the only thing left to do is the view. It should be self explanatory:

    <?php foreach($printLabelPropDecorators AS $prop): ?>
    
        <?php $prop->printLabels(); ?>
    
    <?php endforeach ?>
    

    That could be one approach to do it. The actual logic that decides which labels to print does now live in the Decorator and you are not cluttering your view.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭