dongruan6001 2014-08-30 17:24
浏览 29
已采纳

如何设计Controller,Component,View和Helper之间的关系

I have a trouble with software design. I want to design a relation between Controller, Component, View and Helper. Ihave these models Aricle, Product, List . And in contents of Article and Product there are lists tags in form of [LIST=1].

So I'd like to render these lists and inject them into content in place of tags. I thought that good idea would be a ListRendererHelper which would render lists inside of View like this $this->ListRenderer->renderList($listId). And it would be a good idea if I list ids in database, but I should obtain it from content. And this is not good practice to manipulate with objects inside of View nor insert a lot of PHP code in View.

So I think that best place to manipulate content is Controller, and here obtain the lists ids. But I shouldn't use Helper in Controller so I think that I should make a Component for Controller. And here is my problem, because I will have to write render code twice in Component and in Helper which is against DRY. Other solution is to call Helper inside Component but that would be a MVC break too. So anyone have an idea how to design it?

  • 写回答

1条回答 默认 最新

  • douyu9012 2014-08-30 17:35
    关注

    Manipulating data in a controller and render code in a component? Calling a helper inside a component? What the...? From where did you get these plain wrong ideas? You've understood MVC completely wrong.

    You want a model method to get your data or to manipulate data. All data fetching and maniplation should happen in the model layer.

    MyModel::getSomeFancyData($arg1, $arg2);
    MyModel::manipulateMyFancyData($data);
    

    The controller just passes the data along to the view. Controllers should be always as slim as possible.

    $this->set('fancyData', $this->Model->getSomeFancyData());
    

    Inside the view use a helper for recurring view logic. If you need to render large chunks for HTML that repeat better use an element or call that element from inside a helper.

    I suggest you to read about the basics of MVC and CakePHP:

    This isn't specific to CakePHP, you won't have a benefit from any framework if you're not able to apply the basic concepts like MVC to them.

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

报告相同问题?

悬赏问题

  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成
  • ¥15 HC32L176调试了一个通过TIMER5+DMA驱动WS2812B
  • ¥15 cocos的js代码调用wx.createUseInfoButton问题!
  • ¥15 关于自相关函数法和周期图法实现对随机信号的功率谱估计的matlab程序运行的问题,请各位专家解答!
  • ¥15 Python程序,深度学习,有偿私
  • ¥15 扫描枪扫条形码出现问题
  • ¥35 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型