ThinkAdmin V4.0(thinkphp5.1.35) swoole整合记录

Jackey PHP 6,083 次浏览 , 没有评论

源码为ThinkAdmin V4.0(thinkphp5.1.35)
为了适合win做开发,环境采用的docker centos7.6

踩坑记录:

1.安装topthink/think-swoole报错:

The "https://packagist.laravel-china.org/packages.json" file could not be downloaded: Peer certificate CN=*.phphub.org' did not match expected CN=packagist.laravel-china.org'
修改当前工程的源地址(仅修改当前工程的,本人尝试过修改全局,无效果
仅修改当前工程配置,仅当前工程可使用该镜像地址:composer config repo.packagist composer https://mirrors.aliyun.com/composer/
取消配置:composer config --unset repos.packagist

2.swoole版本

起初安装的是swoole4.4.0,运行时报错:Uncaught think\Exception: method not exists:think\Request->filterValue
经过查找发现问题:

开启RuntimeHook时, 将替换函数array_walk, array_walk_recursive为swoole实现的版本

这是swoole4.4.0新特性里面的说明,因为array_walk_recursive函数的替换,导致源码无法运行,需降低swoole的版本为:swoole4.3.6
指定版本安装方法:pecl install https://pecl.php.net/get/swoole-4.3.6.tgz
这里查找指定的版本列表:https://pecl.php.net/package/swoole

3.执行pecl install swoole报错

No releases available for package "pecl.php.net/swoole"
可先执行:pecl search swoole
根据提示做update操作

4.报错:ERROR: unable to unpack /tmp/pear/download/swoole-4.4.3.tgz

下载不完整导致,可删除已下载的源码包,重新下载。

5.swoole里面不能够使用thinkphp里面原生路由的写法

如:gopher.cc/s=index/test/test
必须写成:gopher.cc/index/test/test
ThinkAdmin里面,\public\static\plugs\plupload\build.js ajax请求时写死的,带有s=,需要自己去掉才行。

6.不支持request获取get或post值

修改方法:
①不适用request,get就是get,post就是post
②修改think-swoole代码
\vendor\thinkphp\library\think\Request.php
添加方法:

  1. public function withRequest(array $request)
  2. {
  3. $this->request = $request;
  4. return $this;
  5. }

\vendor\topthink\think-swoole\src\Application.php
  1. $this->request->withHeader($header)
  2. ->withServer($_SERVER)
  3. ->withGet($_GET)
  4. ->withPost($_POST)
  5. ->withCookie($_COOKIE)
  6. ->withInput($request->rawContent())
  7. ->withFiles($_FILES)
  8. ->setBaseUrl($request->server['request_uri'])
  9. ->setUrl($request->server['request_uri'] . (!empty($request->server['query_string']) ? '&' . $request->server['query_string'] : ''))
  10. ->setHost($request->header['host'])
  11. ->setPathinfo(ltrim($request->server['path_info'], '/'));

修改为:

  1. $this->request->withHeader($header)
  2. ->withServer($_SERVER)
  3. ->withGet($_GET)
  4. ->withPost($_POST)
  5. ->withRequest($_GET ?? $_POST)
  6. ->withCookie($_COOKIE)
  7. ->withInput($request->rawContent())
  8. ->withFiles($_FILES)
  9. ->setBaseUrl($request->server['request_uri'])
  10. ->setUrl($request->server['request_uri'] . (!empty($request->server['query_string']) ? '&' . $request->server['query_string'] : ''))
  11. ->setHost($request->header['host'])
  12. ->setPathinfo(ltrim($request->server['path_info'], '/'));

 

7.修改文件:\vendor\topthink\think-swoole\src\Application.php
$response->write($content);
修改为:

  1. if ($content != ""){
  2. $response->write($content);
  3. }

不然在操作的时候,swoole日志里面会是满满的报错信息

8.修改文件:\public\static\admin.js

  1. $body.on('submit', 'form.form-search', function () {
  2. var url = str.replace(/&?page=\d+/g, ''); var split = url.indexOf('?') === -1 ? '?' : '&';
  3. if ((this.method || 'get').toLowerCase() === 'get') {
  4. return window.location.href = '#' + $.menu.parseUri(url + split + $(this).serialize());
  5. }
  6. $.form.load(url, this, 'post');
  7. });

修改为:
  1. $body.on('submit', 'form.form-search', function () {
  2. var str = $(this).attr('action').replace('&', '?');
  3. var url = str.replace(/&?page=\d+/g, ''); var split = url.indexOf('?') === -1 ? '?' : '&';
  4. if ((this.method || 'get').toLowerCase() === 'get') {
  5. return window.location.href = '#' + $.menu.parseUri(url + split + $(this).serialize());
  6. }
  7. $.form.load(url, this, 'post');
  8. });

不然数据条件搜索的时候回报错

接着修改:
  1. $.vali.listen = function () {
  2. $('form[data-auto]').map(function () {
  3. if ($(this).attr('data-listen') !== 'true') $(this).attr('data-listen', 'true').vali(function (data) {
  4. var call = $(this).attr('data-callback') || '_default_callback';
  5. var type = this.getAttribute('method') || 'POST', tips = this.getAttribute('data-tips') || undefined;
  6. var time = this.getAttribute('data-time') || undefined, href = this.getAttribute('action') || window.location.href;
  7. $.form.load(href, data, type, window[call] || undefined, true, tips, time);
  8. });
  9. });
  10. };
修改为:
  1. $.vali.listen = function () {
  2. $('form[data-auto]').map(function () {
  3. if ($(this).attr('data-listen') !== 'true') $(this).attr('data-listen', 'true').vali(function (data) {
  4. var call = $(this).attr('data-callback') || '_default_callback';
  5. var type = this.getAttribute('method') || 'POST', tips = this.getAttribute('data-tips') || undefined;
  6. var time = this.getAttribute('data-time') || undefined, href = this.getAttribute('action') || window.location.href;
  7. href = href.replace("&", "?");
  8. $.form.load(href, data, type, window[call] || undefined, true, tips, time);
  9. });
  10. });
  11. };
不然弹窗保存的时候会报错
9.session配置
在session.php配置文件中,添加
'use_swoole_table' => true,
在swoole.php配置文件中需要添加定义:
  1. 'table' => [
  2. // 定义最大记录数
  3. 'size' => 1024,
  4. // 字段类型定义(目前仅支持 string int 和 float类型)
  5. 'column' =>[
  6. 'data' => ['string',255], // 字符串类型 长度为255个字节
  7. 'expire'=> ['int',8], // 整型 长度为8
  8. ],
  9. ],

 

10.登录超时时间
登录超时是根据cookie的有效时间计算的,在配置文件cookie.php里面修改。修改完成之后,需要删除浏览器已生成的cookie,重新生成才能生效。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

Go