douvcpx6526 2013-09-27 06:38
浏览 40
已采纳

在Views / site / index中包含另一个控制器的视图

I can't figure out how to solve this problem i'm facing. Alright, so basically i have a default view/site/index.php view file which is generating the home page. Now i have built two separated Models and their associated Controllers and views. I want to include those view of the controllers in the default home page view i.e. views/site/index.php. Does it make any sense ?

So my home page is divided into several sections. The header and footer parts are the same throughout the whole site, which is why i've written them in the /themes/brushed/layouts folder under separate files. Those are being loaded fine.

The two controller i was talking about are named as FrontPageCategoriesController.php and TopProductsController.php. They are generating their separate view files i.e. index.php in their respective view folders.

How will i include those view files (index.php) into the main site view file (views/site/index.php). I just can't include those files using include_once() because the data is being generated yet by its controller. So whats the way to achieve this ?

Can anyone please help me out with this. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dougou5844 2013-09-27 07:26
    关注

    First you should move data generation from controller to model. Controller should only wire your input parameters to model and pass it to view. The reason is that you want to reuse models, not controllers. Remember that controller is bound to page request, while model is not.

    Then just use your model to fetch the data and renderPartial to render the data. For example:

    $categories = FrontPageCategories::model()->findAll();
    $this->renderPartial('/frontPageCategories/index', array(
        'categories' => $categories,
    ));
    

    In fact it is possible to fetch categories from model directly in view, if they are not dependant on some parameters in the current page.

    Actually, you should use renderPartial for your header and footer too.

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题