douwu8524 2013-03-31 10:22
浏览 32
已采纳

每页一个View对象? 或只是一个View对象加载不同的模板?

In my application I have one View object which can load($template) a template to get it ready to be rendered using the render() method.

In all the stuff I have read about Views sometimes I see people doing it the way I currently do it and other times I see people doing something like the following

class ProductView extends View {

}

class CheckoutView extends View {

}

Having a separate View per page.

What are the advantages/disadvantages of both ways?

Any advice would be great thanks.

  • 写回答

3条回答 默认 最新

  • dongpaozhi5734 2013-03-31 10:55
    关注

    View should not be "loading templates". If you have code like $this->load('header'); in the view, then it is a violation of SRP. View (sometimes) uses templates to create a response for the user, but it should not be creating them.

    In the most of the articles that you will encounter about view, you will read about the interpretation of view from Rails (ands its clones). In RoR you do not have view. Only a glorified dumb template.

    The structure of presentation layer makes it preferable for you to have 1:1 relation between controllers and view. Each view becomes responsible for handing of the response that has been triggered by user's input. Therefore: you page would usually have a one view per execution time.

    In more complex cases you might want to look into concept of composite view. This approach lets you separate the the presentation logic in smaller chunks. This also makes it much easier to structure your code, if you user ViewModel.

    The bottom line is this: if you have one view per whole application, then you are doing it wrong.

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

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私