douwanc63652 2014-06-12 20:56
浏览 28
已采纳

PHP MVC - 我究竟如何使用视图?

I'm trying to learn the MVC design pattern for PHP development and I have a couple of questions concerning view structure.

  1. I keep seeing "Views are not templates!" plastered everywhere where I read about MVC. But how do I include stuff like headers, navbars and footers? If I have html that should be shared by all views, how do I include it around the view without a template?

  2. I also read on another thread in stack overflow that views should never have require statements, and that they are the ones that should be required. But what about a case where I have a view that should be required in multiple views? Let's say that some views have a few lines that spit out a list, it would be more logical to reuse the code using require statements within a view than to rewrite it for every view, but this is apparently against the nature of views.

Thanks.

  • 写回答

1条回答 默认 最新

  • douping7105 2014-06-12 21:05
    关注

    A template isn't a view, but a view could use a template. Templates are just re-usable architecture/design that a view can be based on. Views are typically a specific page or page type.

    Views should also have little to no business logic in it, and should be focused on the layout and design, hence why there should be little or no requires.

    If your view is using a template, then you likely don't need to include things like footers and headers, depending on the template system you are using. But if you are creating the view without a template, you will likely have to include those on the view yourself.

    Focus on maintainability, scalability and a separation of design and logic, and make your code re-usable whenever possible.

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

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?