douqianzha6213 2009-08-12 22:05
浏览 54
已采纳

在Zend Framework中的视图或控制器中设置变量是否更好?

My question needs a bit of setup, so please bear with me:

I became a convert to using View Helpers for getting data from a model rather than sprinkling it all over the controllers (hat tip to Eric Clemmons). It's much more reusable and flexible there. I just love it!

What I usually do, is lay out the template in the index.phtml, and then when I need to get something from a model, put that snippet in a detail.phtml, so logic is as far out of the way as possible.

However, I start to see the need for variables that get reused. For example, the category name. Now you don't want to use a view helper to get the cat name from a model over and over again. Although you can cache it, it's obviously way too big of a hassle.

So I started using a couple of lines of php in the detail.phtml to set variables. And it doesn't smell right anymore. Views shouldn't have too much logic.

So what say you all? If the var gets reused, put it in the controller? Or don't mind a couple vars set in the view?

EDIT: Alan Storm asked for an example of viewhelpers:

detail.phtml:

<ul id="productList">

<? foreach($this->getProductById($id) as $product) : ?>
    <li><?= $this->escape($product['name']) ?></li>
<? endforeach; ?>

</ul>

(bracing myself for attack of the anti-short-taggers)

ANOTHER EDIT: I see there can't be 2 right answers. Oh well...

  • 写回答

7条回答 默认 最新

  • dongyi9330 2009-08-12 23:19
    关注

    I'm not sure the exact technique you're talking about. The following assumes you're creating methods on View Helpers that return information by wrapping calls to your model's "get data" methods. This gets you away from the Passive View pattern employed by many other PHP MVC Frameworks. The views are going directly to the models for their data. Your concers is multiple calls to the the view helper will cause the model to fetch the data twice. This is a potential performance problem that seems easily avoidable.

    //example of how I'm assuming you're using view helpers
    echo $this->viewHelperName->modelName->getDataIWant('key');
    

    If that accurately describes your problem, the "if I need to use it twice, set a view variable in the controller, otherwise just use the view helper" approach is probably the wrong one. This is more my personal preference, but whichever approach you choose for getting data from the models to your view, you should stick to throughout your application.

    The problem you're trying to solve here is "fetching data directly from the models has a high performance cost". This is a problem that the model implementation should fix. It's not something that should be fixed by janky coding style :)

    The best solution, as you've already mentioned, is caching. Caching doesn't have to be "too big of a hassle" if you're smart about it.

    public function getDataIWant($key, $clear_cache=false)
    {
        if(!array_key_exists($key, $this->_cache) || $clear_cache)
        {
            $this->_cache[$key] = parent::getDataIWant[$key];
        }
    
        return $this->_cache[$key];
    }
    

    If caching isn't viable for the way you're using models, then I'd suggest adding a method to your model for getting the data you want, and using extract to define variables in the view scope.

    class MyModel
    {
        ...
        function getDataForFooView
        {
            return Array(
                'company_name'=>$this->getCompanyName
            );
        }
        ...
    }
    
    ...
    //top of the view file
    <?php extract($this->viewHelper->modelName->getDataForFooView()) ?>
    <h1><?php echo $company_name; ?></h1>
    

    It's still kind of janky, but you get a consistency in your view files (a single line at the top) that will smell less. That said, caching is "The Right™" way to do this. Avoiding that is just trading one smell for another. One way to think about when you define a view variable (either directly in the view or by setting it in the controller) you're already using caching, just in a haphazard fashion.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line