二次开发文档:第三方类
引入ThinkPHP框架
宸逸框架支持接入ThinkPHP6框架语法,开发者可以使用ThinkPHP的操作语法
针对宸逸CMS4.6以下版本(即4.5.*)可用,4.6及其以上版本已经内置了ThinkPHP则无需本教程。
=============手动下载ThinkPHP6方式==============
1、下载ThinkPHP6官方源码
也可以使用宸逸的第三方类仓库:https://gitee.com/dayrui/composer,将下载的文件放到dayrui/Vendor/目录中。
2、将下载的源码包vendor复制
3、复制到 宸逸框架/dayrui/Vendor (注意首字母需要大写)
=============宝塔傻瓜式下载ThinkPHP6方式==============
1、在宝塔站点中,选择
执行命令:
composer require topthink/think-orm
然后执行目录:加上dayrui目录
执行完成:
=================开发代码写法示例=====================
1、开发测试数据库查询语法
新建控制器文件:dayrui/App/Demo/Controllers/Tp.php
where('id', 1)->find(); var_dump($rt); } }
使用Thinkphp查询member标签 id=1 的数据
2、访问控制器地址
/index.php?s=demo&c=tp
已经完美的输出查询数据,表示接入ThinkPHP已经成功~
自定义类方法文件
宸逸CMS框架可以自动识别和加载第三方类方法,开发者需要按要求将自己的类定义好,直接可以在控制器中执行该方法,不需要加载,程序会自动识别加载。
第三方类方法定义目录格式:
dayrui/ThirdParty/类名.php
类名:首字母必须大写,其他字母小写
1、创建类方法文件
dayrui/ThirdParty/Test.php
2、在控制器中执行方法
dayrui/Fcms/Control/Test.php
3、访问执行结果
/index.php?c=test
Swoole接入
Swoole 使 PHP 开发人员可以编写高性能高并发的 TCP、UDP、Unix Socket、HTTP、 WebSocket 等服务,让 PHP 不再局限于 Web 领域。
本教程以WebSocket为例。
一、安装Swoole4
宝塔面板为例,在php设置中直接可以安装
二、测试程序脚本
1、在web目录中新建文件/api/sms.php (注意文件名大小写格式)
2、再到xunruicms的主程序目录建立业务处理文件 /dayrui/My/Api/Sms.php (注意文件名大小写格式)
//创建websocket服务器对象,监听0.0.0.0:9502端口 $ws = new Swoole\WebSocket\Server("0.0.0.0", 9502); //监听WebSocket连接打开事件 $ws->on('open', function ($ws, $request) { var_dump($request->fd, $request->get, $request->server); $ws->push($request->fd, "hello, welcome\n"); }); //监听WebSocket消息事件 $ws->on('message', function ($ws, $frame) { echo "Message: {$frame->data}\n"; $ws->push($frame->fd, "server: {$frame->data}"); }); //监听WebSocket连接关闭事件 $ws->on('close', function ($ws, $fd) { echo "client-{$fd} is closed\n"; }); $ws->start();这个是swoole官方的例子
3、使用命令行工具执行文件:
php 网站根目录/api/sms.php
如果这种样子表服务已经正常启动了
4、安全组放行端口号:9502
5、宝塔防火墙放行端口号:9502
6、使用swoole官方例子在任意html文件中加上客户端测试代码
视频提取缩略图
提取视频文件中的缩略图片方法
首先安装ffmpeg
1、BT面板的linux支持一键安装
wget http://download.bt.cn/install/ext/ffmpeg.sh && sh ffmpeg.sh
2、运行完成后执行:
ffmpeg -version
3、回到BT面板的PHP设置,解禁函数
把shell_exec从列表中删除
4、在PHP中写代码编写生成缩略图代码
// 测试代码相对于控制器 $aid = 123; // aid表示附件id号 $info = \Phpcmf\Service::C()->get_attachment($aid); if (is_file($info['file'])) { $sell = 'ffmpeg -i '.$info['file'].' -r 30 -t 4 '.$info['file'].'.jpg'; shell_exec($sell); // 这样就生成了一个.jpg的后缀缩略图 echo $info['url'].'.jpg'; } else { // 文件不存在于本地磁盘 }
html转换为word文档doc格式
代码仓库:https://github.com/cshaptx4869/html2word
1、进入/dayrui目录
2、使用命令行安装类
composer require cshaptx4869/html2word
3、需要将vendor目录命名为Vendor(首字母大写)
4、新建html测试文件
dayrui/App/Demo/doc.html
html template 中文的标题,技术无止境,一直在路上
p是可以分段的. 使用PHP将html转word
再分一段 使用PHP将html转word
还分一段,下面加个图片
4、新建控制器文件:
dayrui/App/Demo/Controllers/Doc.php
addFile($tpl)->download("这是一个名称"); } }
5、访问url,即可下载doc文件
index.php?s=demo&c=doc&m=index
导入和导出Excel表格文件
代码仓库:https://github.com/PHPOffice/PhpSpreadsheet
1、进入/dayrui目录
2、使用命令行安装类
composer require phpoffice/phpspreadsheet
也可以使用宸逸的第三方类仓库:https://gitee.com/dayrui/composer,将下载的文件放到dayrui/Vendor/目录中。
3、需要将vendor目录命名为Vendor(首字母大写)
4、新建控制器文件 :
dayrui/App/Demo/Controllers/Excel.php
'111', 'title2' => '222'], ['title1' => '111', 'title2' => '222'], ['title1' => '111', 'title2' => '222'] ]; $title = ['第一行标题', '第二行标题']; // Create new Spreadsheet object $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->getColumnDimension('A')->setWidth(50);// 给A列设置宽度 $sheet->getColumnDimension('B')->setAutoSize(true); // 给B列设置自动宽度 $sheet->getRowDimension('1')->setRowHeight(30);// 给第一行设置行高,一般第一行是字段名称 // 设置第一行背景和居中等 $abc = array('A', 'B', 'C', 'D', 'E', 'F', 'G'); // 有多少列就列多少字母 foreach ($abc as $a) { $sheet->getStyle($a.'1')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setRGB("efefef"); $sheet->getStyle($a.'1')->getFont()->setBold(true); $sheet->getStyle($a.'1')->getFont()->setSize(14); // 第一行居中 //$sheet->getStyle($a.'1')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER); // 第一行居中对齐 $sheet->getStyle($a.'1')->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER); } $sheet->setTitle('导出结果');// 工作表格名称 // 方法一,使用 setCellValueByColumnAndRow //表头 //设置单元格内容 foreach ($title as $key => $value) { // 单元格内容写入 $sheet->setCellValueByColumnAndRow($key + 1, 1, $value); } $row = 2; // 从第二行开始 foreach ($data as $item) { $column = 1; foreach ($item as $value) { // 单元格内容写入 $sheet->setCellValueByColumnAndRow($column, $row, $value); $column++; } $row++; } // Redirect output to a client’s web browser (Xlsx) header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="01simple.xlsx"'); header('Cache-Control: max-age=0'); // If you're serving to IE 9, then the following may be needed header('Cache-Control: max-age=1'); // If you're serving to IE over SSL, then the following may be needed header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified header('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header('Pragma: public'); // HTTP/1.0 $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx'); $writer->save('php://output'); exit; } // 导出 方法二,使用 setCellValue public function index2() { $data = [ ['title1' => '111', 'title2' => '222'], ['title1' => '111', 'title2' => '222'], ['title1' => '111', 'title2' => '222'] ]; $title = ['第一行标题', '第二行标题']; // Create new Spreadsheet object $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); // 方法二,使用 setCellValue //表头 //设置单元格内容 $titCol = 'A'; foreach ($title as $key => $value) { // 单元格内容写入 $sheet->setCellValue($titCol . '1', $value); $titCol++; } $row = 2; // 从第二行开始 foreach ($data as $item) { $dataCol = 'A'; foreach ($item as $value) { // 单元格内容写入 $sheet->setCellValue($dataCol . $row, $value); $dataCol++; } $row++; } // Redirect output to a client’s web browser (Xlsx) header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="01simple.xlsx"'); header('Cache-Control: max-age=0'); // If you're serving to IE 9, then the following may be needed header('Cache-Control: max-age=1'); // If you're serving to IE over SSL, then the following may be needed header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified header('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header('Pragma: public'); // HTTP/1.0 $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx'); $writer->save('php://output'); exit; } // excel文件读取 public function read() { $file = "excel.xls"; // 这里写路径 $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file); $sheet = $spreadsheet->getActiveSheet(); $res = []; // 读取excel文件表格 foreach ($sheet->getRowIterator(1) as $row) { $tmp = []; foreach ($row->getCellIterator() as $cell) { $tmp[] = $cell->getFormattedValue(); } $res[$row->getRowIndex()] = $tmp; } print_r($res); } }
5、访问url,即可下载excel文件
index.php?s=demo&c=excel&m=index
具体参数配置参考PhpOffice官方的文档介绍
https://phpspreadsheet.readthedocs.io/
Phpword生成word文档
代码仓库:https://github.com/PHPOffice/PHPWord
1、进入/dayrui目录
2、使用命令行安装类
composer require phpoffice/phpword
也可以使用宸逸的第三方类仓库:https://gitee.com/dayrui/composer,将下载的文件放到dayrui/Vendor/目录中。
3、需要将vendor目录命名为Vendor(首字母大写)
4、新建test.docx作为测试的模板文件,并设置变量
变量:${name} 职位:${zhiwei} 编号:${bianhao}
5、新建控制器文件:
dayrui/App/Demo/Controllers/Doc.php
setValue('name', '姓名'); $templateProcessor->setValue('zhiwei', '公务员'); $templateProcessor->setValue('bianhao', '360281199909090009'); // 保存新的文件 $templateProcessor->saveAs('test2.docx'); } }
5、访问url,即可生成test2.docx文件
index.php?s=demo&c=doc&m=index