上一文只研究了JImage类,今天继续其他常用的joomla内置类,个人是从常用角度来写的,如果PHP本身函数比起Joomla内置类用起来更方便的,我就滤过不说,如果你实在想用,自己去查吧,个人觉得,无论多优秀的方法,如果PHP自带函数也能很好解决,那么用PHP内置函数是最好的,学习PHP首先就是学好内置函数,好了,接下来详细讲解那些常用的类以及方法.
1.文档类,用来对当前页面元素的头部作为主要操作.
<span>$document</span> = JFactory::<span>getDocument(); </span><span>$document</span>->addScript('1.js');<span>//</span><span>添加js文件</span> <span>$document</span>->addScriptDeclaration('alert(1)');<span>//</span><span>添加js代码</span> <span>$document</span>->addStyleDeclaration(); <span>//</span><span>css</span> <span>$document</span>->addStyleSheet('1.css'<span>); </span><span>$document</span>->setMetaData('description','关键字');<span>//</span><span>设置描述或关键字</span> <span>$document</span>->setCharset('utf-8'<span>); </span><span>$document</span>->setTitle('标题');
2.文件操作,需要导入jimport('joomla.filesystem.file');
<span>$j</span> = <span>new</span><span> JFile(); getExt(</span><span>$file</span>) <span>//</span><span>获取扩展名不含.</span> getName(<span>$file</span>) <span>//</span><span>获得文件名</span> exists(<span>$file</span>) <span>//</span><span>文件是否存在</span> delete(<span>$file</span>) <span>//</span><span>删除文件</span> <span>copy</span>(<span>$old</span>,<span>$new</span>) <span>//</span><span>复制</span> upload(<span>$src</span>) <span>//</span><span>上传</span> write(<span>$file</span>) <span>//</span><span>写文件</span> read(<span>$file</span>) <span>//</span><span>读文件</span> move(<span>$old</span>,<span>$new</span>) <span>//</span><span>移动文件</span>
这些方法在文件读写,图片文件上传,等都有用处.
3.文件夹操作,jimport('joomla.filesystem.folder');一般我们导入的时候把文件文件夹都导入,很多时候都会用到
JFolder::create(<span>$path</span>,777) <span>//</span><span>创建文件夹,支持多层</span> JFolder::delete(<span>$path</span>) <span>//</span><span>删除文件夹,可以包含文件</span> JFolder::exists(<span>$path</span>) <span>//</span><span>检测文件夹是否存在</span> JFolder::<span>copy</span>(<span>$pathold</span>,<span>$pathnew</span>) <span>//</span><span>复制文件夹</span> JFolder::files(<span>$path</span>) <span>//</span><span>列出文件夹下面文件的数组</span> JFolder::folders(<span>$path</span>) <span>//</span><span>列出文件夹下面文件夹的数组</span>
这里说一下::是静态方法,有很多类是支持静态方法的;
4.图标生成类,用来生成后台的小图标,常用的保存就是之一.
JToolBarHelper::title(JText::_('添加个人信息'), 'weblinks.png');<span>//</span><span>标题</span> JToolBarHelper::addNew('info.add');<span>//</span><span>添加</span> JToolBarHelper::apply('info.apply');<span>//</span><span>保存</span> JToolBarHelper::save('info.save');<span>//</span><span>保存关闭</span> JToolBarHelper::editList('info.edit');<span>//</span><span>编辑</span> JToolBarHelper::cancel('info.cancel');<span>//</span><span>取消</span> JToolBarHelper::deleteList('info.delete');<span>//</span><span>删除</span> JToolBarHelper::trash('info.trash');<span>//</span><span>回收站</span> JToolBarHelper::divider();<span>//</span><span>分割线</span> JToolBarHelper::publish('weblinks.publish', 'JTOOLBAR_PUBLISH', <span>true</span>);<span>//</span><span>发布</span> JToolBarHelper::unpublish('weblinks.unpublish', 'JTOOLBAR_UNPUBLISH', <span>true</span>);<span>//</span><span>取消发布</span> JToolBarHelper::back();<span>//</span><span>返回</span> JToolBarHelper::help('JHELP_COMPONENTS_WEBLINKS_LINKS');<span>//</span><span>帮助</span>
5.常用其他方法
JString::transcode('中国','utf-8','gb2312');<span>//</span><span>转换编码</span> JUtility::getToken();<span>//</span><span>用户session ID 等价于JHtml::_('form.token');</span> JUtility::isWinos();<span>//</span><span>是否win系统 是输出1</span> JUtility::sendmail('logove#qq.com','yoby','logoove#foxmail.com','主题','这是内容');<span>//</span><span>发送邮件要配置的</span> JUtility::gethash(<span>$var</span>);<span>//</span><span>32位随机字符</span> JUtility::return_bytes('1k');<span>//</span><span>转换成字节数 支持g m k</span> JRequest::getURI();<span>//</span><span>获得当前url</span> JRequest::getMethod ();<span>//</span><span>提交方式 POST 或GET</span> JRequest::getbool('i');<span>//</span><span>转换成布尔型</span> JRequest::getint('i');<span>//</span><span>取整</span> JRequest::getfloat('i');<span>//</span><span>转换成浮点数</span> JRequest::getString('i');<span>//</span><span>转成字符串</span> JRequest::getvar('var',<span>null</span>,'default','array');<span>//</span><span>通用,第二个是默认值,第三个是方法,有GET //POST FILES COOKIE ENV SERVER 最后一个是数据类型 有array int string float</span> JRequest::setVar('var',<span>$arr</span>);<span>//</span><span>设置,可以数组或字符</span> JURI::base();<span>//</span><span>包含administrator绝对路径 参数1是相对路径</span> JURI::root();<span>//</span><span>根目录,参数1是相对路径</span>
6.数组常用,这个是最常用的
JArrayHelper::arrayUnique(<span>$arr2</span>);<span>//</span><span>去掉重复元素</span> <span>$obj</span> = <span>new</span><span> stdClass(); </span><span>$obj</span>->qq='280594236'<span>; JArrayHelper</span>::fromObject(<span>$obj</span>);<span>//</span><span>对象转换数组</span> JArrayHelper::getValue(<span>$arr</span>,1);<span>//</span><span>获得数组第二个值,也可以是下标</span> JArrayHelper::toObject(<span>$arr</span>);<span>//</span><span>转换对象</span> JArrayHelper::toString(<span>$arr</span>,'=',' ');<span>//</span><span>转成字符串,空格隔开,左边key=value</span> JArrayHelper :: toInteger(<span>$cid</span>);<span>//</span><span>转换变量为整形</span>
其他还有很多的方法,比如数据库,xml,表单生成等等,这些说起来都很重要,不过内容过多,在后边有机会再讲,今天的这些方法,基本上来说都很常用,需要了解更多的你需要自己去研究api内容,或者自己读核心代码,我在这里总结出来,可以减少你去阅读时间,后边的注释说明很详细,不懂的可以在下面留言,joomla是一款非常优秀的CMS,不过国内并不流行,dedecms和discuz用户量更大,下次有时间我也会去研究一下的,喜欢cms的不要错过了.
(yoby 原创)