公司需要用phpcms构建一个官网,作为一个学过php的菜鸟自然而然被招募进来。以前培训过半年的php,并且自己用tp框架写了一个简单的博客系统。但是对于phpCMs确实不熟悉。领到任务后马上行动,查资料,看源码加讨论,终于如期的在周一上线了官网系统。这里总结一下使用phpCMS的经验。
1:phpcms和tp 框架一样也是基于MVC框架的,并且做了更多的封装。Phpcms可以在view中写pc标签直接写操作数据库的代码,直接从数据库中读数据,如下:
{pc:content action="lists" catid="10" order="updatetime DESC" thumb="1" num="4" return="infor"}
{loop $infor $v}
<?php var_dump($v) ?>
<div class="portfolio">
<div class="portfolio-wrapper">
<a class="b-animate-go" href="{$v[url]}">
<img style="width: 100%;" src="{$v[thumb]}" alt="" />
<div class="b-wrapper">
<p class="b-animate"><img src="{IMG_PATH}dh_img/link.png" /></p>
</div>
</a>
</div>
<div class="bottom-header">
<h5>{$v[title]}</h5>
<p class="newsIntroduction">{$v[description]}
</p>
</div>
</div>
{/loop}
{/pc}
执行流程如下:
①模块名:content
②模块类文件:modules/content/classes/content_tag.class.php,便于二次开发或标签的扩展
③文件中的方法:lists
④action后面的参数以数组的形式传递给lists方法,如:function lists($data=array())
此标签实际获取的是:content_tag.class.php文件中lists方法返回的数据
注意:此标签会根据catid的值去寻找对应的模型id,然后再根据对应的模型id寻找到对应的模型表,如:news
注意:几乎所有的模块都有一个标签类,格式如下:模块名_tag.class.php
注意:前台模板文件中出现{pc}标签的位置,都可在”碎片管理”中进行编辑修改,非常方便(参考:
http://www.cnblogs.com/yuwensong/archive/2013/03/06/2946302.html);
2:将 字符串转成数组函数string2array;这是phpcms自己封装的一个函数,可以将数组形式的字符串转成数组,也可以将json形式的字符串转成JSON对象。这个函数在我们读取多图片上传的字段的时候很有用,因为根据版本的不同,phpcms会将多图片的路径存成一个数组形式的字符串或者是一个json样式的字符串。
3:修改模型。
Phpcms默认定义了四种模型,分别是文章模型,图片模型,下载模型,视频模型。在我们的官网中基本都用的是文章模型,但是发现文章模型不能满足我们的需求,比如文章模型只能上传一张缩略图,而我们的文章还需要附带几张图片。所以我们修改了文章模型的字段,添加了一个类型为images的字段,添加完后,系统就自动在添加内容时候显示出来,并且将你上传的图片的路径存到这个字段下面。
4:读取v9_news_data这个附表的数据。
要想把v9_news和v9_news_data的数据一起读出来,只需要在pc标签中加入‘moreinfo’=1这个属性就可以了。
5:循环读出一级二级标题:
{loop subcat(0,0,0,$siteid) $r}
<li class="">
<div class="link"><i class="fa fa-angle-right"></i>{$r['catname']}</div>
<?php $childCatIdArr=explode(',',$r['arrchildid']);?>
{if count($childCatIdArr)>1}
{pc:content action="category" catid="$r[arrchildid]" num="20" siteid="$siteid" order="listorder ASC"}
{loop $data $r}
<div class="sublink_1"><i class="fa fa-angle-right"></i>{$r['catname']}</div>
{pc:content action="lists" catid="$r['catid']" num="999" order="listorder ASC" return="info"}
<ul class="submenu submenu_1">
{loop $info $v}
<li><a href="{$v['url']}" title="{$v['title']}">{str_cut($v['title'],40)}</a></li>
{/loop}
</ul>
{/loop}
{/pc}
{else}
{loop $childCatIdArr $child}
{pc:content action="lists" catid="$child" num="999" order="listorder ASC" return="info"}
<ul class="submenu">
{loop $info $v}
<li><a href="{$v['url']}" title="{$v['title']}">{str_cut($v['title'],40)}</a></li>
{/loop}
</ul>
{/pc}
{/loop}
{/if}
</li>
{/loop}
这里主要用到了phpcms中自定义的函数subcat(),这个函数定义在phpcms/libs/functions/global.func.php文件中:函数原型如下:
function subcat($parentid = NULL, $type = NULL,$self = '0', $siteid = '') {
if (empty($siteid)) $siteid = get_siteid()
$category = getcache('category_content_'.$siteid,'commons');
foreach($category as $id=>$cat) {
if($cat['siteid'] == $siteid && ($parentid === NULL || $cat['parentid'] == $parentid) && ($type === NULL || $cat['type'] == $type)) $subcat[$id] = $cat;
if($self == 1 && $cat['catid'] == $parentid && !$cat['child']) $subcat[$id] = $cat;
}
return $subcat;
}
顶级标题的parentid是null,所以subcat的第一个参数是0的时候,获得的就是定义标题。