3.2版跨模块及动态Include解决方案(大道至简)

浏览:2240 发布日期:2016/06/24 分类:技术分享 关键字: 3.2 动态 跨模块 Include
以前来提问过,版主说用Widget解决,发现比较麻烦,而且代码和结构都不是很优雅,因为我想用Controller来统一控制权限。于是乎,经过无数次折腾,找到了一个自己比较满意的解决方案,记录一下,希望能够帮到需要的人。

下面以header和footer为例:

1、建立IncludeControllernamespace Admin\Controller;
use Think\Controller;
class IncludeController extends Controller {
    public function header(){
        //测试动态数据
        $Test = M('system_user');
        $data = $Test->select();
        $this->assign('data',$data);
        //大招:这里必须用T方法
        $this->display(T('Admin@Include:header'));
    }
    public function footer(){
        //大招:这里必须用T方法
        $this->display(T('Admin@Include:footer'));
    }
}
2、建立header和footer模板文件<!-- header.html -->
<h1>Include Header</h1>
<?php dump($data); //测试动态数据 ?>
<!-- footer.html -->
<h1>Include Footer</h1>
3、在任意模块的模板中调用header和footer(比如要在Home模块的模板中调用Admin模块中的header和footer)<!-- index.html -->
<?php $Include = A('Admin/Include'); //大招:使用A方法 ?>
<?php $Include->header(); ?>
<h1>Index Content</h1>
<?php $Include->footer(); ?>
哟嚯嚯嚯!是不是很简单很方便?代码管理起来也轻松多了~
最佳答案
评论( 相关
后面还有条评论,点击查看>>