• ThinkPHP Widget模块开发流程


    初识ThinkPHP的Widget,现把模块开发的流程发布如下,也方便以后自己查阅:

    一、新建数据库表self_modules,sql代码如下

    CREATE TABLE `self_modules` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `title` text NOT NULL,
      `content` text NOT NULL,
      `order` int(11) NOT NULL DEFAULT '0',
      `position` varchar(50) NOT NULL,
      `published` tinyint(1) NOT NULL DEFAULT '1',
      `module` varchar(50) NOT NULL,
      `access` tinyint(3) NOT NULL DEFAULT '0',
      `showtitle` tinyint(3) NOT NULL DEFAULT '1',
      `params` text NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
    
    
    INSERT INTO `self_modules` VALUES ('1', '主菜单', '', '0', 'left', '1', 'Menu', '0', '1', 'id=1
    style=17joys');
    INSERT INTO `self_modules` VALUES ('2', '子菜单', '', '0', 'left', '1', 'Menu', '0', '1', 'id=1
    style=menu');
    INSERT INTO `self_modules` VALUES ('3', '最新文章模块', '', '0''left','1','LatestNews','0','1','sid=2
    cid=3
    style=latestnews');

    2、前台项目home/Lib/Action目录下建立ModulesAction.class.php,代码如下

    class ModulesAction extends CommonAction{
        function index(){
            $mod = new Model('Modules');
            $list = $mod->where("position='left'")->select();
            $this->assign('modlist',$list);
            $this->display();    
        }
    }

    3、/home/Tpl/default/Modules目录下建立index.html模板文件,代码如下(注意模块的调用方法)

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>模块</title>
    </head>
    
    <body>
    <volist name="modlist" id="row">
        <h3>{$row['title']}</h3>
        {:W($row['module'],array('params'=>$row['params'],))}
    </volist>
    
    </body>
    </html>

    4、/home/Lib目录下建立Widget目录,在该目录下建立菜单模板文件MenuWidget.class.php,代码如下

    class MenuWidget extends Widget{
        function render($data){
            //return '<font color="'.$data['color'].'">aaa</font>';
            $params = $data['params'];
            $p = explode("
    ",$params);
            $pv = array();
            foreach($p as $v){
                $tmp = explode("=",$v);
                $pv[$tmp[0]] = $tmp[1];    
            }
            //dump($pv);
            
            $menuitem = new Model('MenuItem');
            $list = $menuitem->field("id,name,link,concat(path,'-',id) as bpath")->order('bpath,id')->where('menuid='.$pv['id'])->select();
            foreach($list as $key=>$val){
                $list[$key]['signnum'] = count(explode('-',$val['bpath']))-1;
                $list[$key]['marginnum'] = (count(explode('-',$val['bpath']))-1)*10;
            }
            $data['milist'] = $list;
            $content = $this->renderFile($pv['style'],$data);
            return $content;    
        }
    }

    5、/home/Lib/Widget/目录下建立菜单模块的模板目录Menu,并在该目录下建立菜单模块模板menu.html和17joys.html(这里建两个主要是多套模板可以选择)代码分别如下:

    menu.html

    <ul>
        <volist name="milist" id="row">
            <li style="margin-left:{$row['marginnum']}px"><a href="{$row['link']}">{$row['name']}</a></li>
        </volist>
    </ul>

    17joys.html

    <ol>
        <volist name="milist" id="row">
            <li style="margin-left:{$row['marginnum']}px"><a href="{$row['link']}">{$row['name']}</a></li>
        </volist>
    </ol>

    至此模块开发完成,下面再写一个最新文章模块可以用来练习使用:

    1、/home/Lib/Widget/目录下建立最新文章模块的Widget文件LatestNewsWidget.class.php,代码如下

    class LatestNewsWidget extends Widget{
        function render($data){
            $params = $data['params'];
            $p = explode("
    ",$params);
            $pv = array();
            foreach($p as $v){
                $tmp = explode("=",$v);
                $pv[$tmp[0]] = $tmp[1];    
            }
            
            $article = new Model('Article');
            $list = $article->where('sectionid='.$pv['sid'].' and catid='.$pv['cid'])->select();
            //dump($list);
            $data['alist'] = $list;
            
            $content = $this->renderFile('latestnews',$data);
            return $content;
        }
    }

    2、/home/Lib/Widget/目录下建立最新文章的模板目录LatestNews,并在该目录下建立最新文章模块的模板文件latestnews.html,代码如下:

    <ul>
        <volist name="alist" id="row">
            <li>{:U('Article/view/id/'.$row['id'])}<a href="__APP__/Articl/view/id/{$row['id']}">{$row['title']}</a></li>
        </volist>
    </ul>

    至此最新文章模块完成,其实感觉也挺简单的,就两部,关键点就是处理好控制器模板穿过来的参数:{:W($row['module'],array('params'=>$row['params'],))}

    文章的静态化要在home项目的配置文件配置静态化设置:/home/Conf/config.php

    'URL_HTML_SUFFIX'=>'.html',
    If the copyright belongs to the longfei, please indicate the source!!!
  • 相关阅读:
    robotframework学习笔记七:robotframework的数据驱动模式
    robotframework学习笔记六:Set Variable If 关键字的使用
    robotframework学习笔记五:robotframework控制流if和for
    C# BS方向 该如何规划学习?【学习路线指南】
    毕业四年,我当初是如何走上编程这条路的!
    设计模式之备忘录
    设计模式之中介者
    设计模式之迭代器
    设计模式之命令模式
    nginx 499 错误解决
  • 原文地址:https://www.cnblogs.com/longfeiPHP/p/4942825.html
Copyright © 2020-2023  润新知