记录thinkphp3.13移植到php7过程

hykeda7年前ThinkPHP401

随着php7的兴起,越来越多的公司用php7了,下面记录一次thinkphp3.1.3项目移植到php7解决兼容性的过程。

先在thinkphp论坛搜索了下,搜到最多的答案就是修改Think\Lib\Template\ThinkTemplate.class.php文件,但是依然会有兼容性的问题。下面总结下步骤:

1、Mysql必须切换成PDO的驱动的连接方式。

请查看你的数据库配置文件,更换成下面对应的数据库参数:

	
  1. return array (
  2. 'DB_TYPE' => 'pdo', //原来的mysql换成pdo
  3. 'DB_HOST' => '127.0.0.1',
  4. 'DB_NAME' => 'thinkphp',
  5. 'DB_USER' => 'root',
  6. 'DB_PWD' => '',
  7. 'DB_PORT' => '3306',
  8. 'DB_PREFIX' => '',
  9. 'DB_CHARSET' => 'utf8',
  10. 'DB_DSN' => 'mysql:host=127.0.0.1;dbname=thinkphp;charset=utf8', //这行一定要加
  11. );

2、重定向修改。

打开除了默认主页的其他页面,会发现提示No input file specified.

下面是百度到的答案,博主用的是apache,index后面加上了?就搞定。

No input file specified修改

(一)IIS Noinput file specified

方法一:改PHP.ini中的doc_root行,打开ini文件注释掉此行,然后重启IIS

方法二: 
请修改php.ini找到 
; cgi.force_redirect = 1 
去掉前面分号,把后面的1改为0 
即 
cgi.force_redirect = 0

(二)apacheNo input file specified

apache No input filespecified,今天是我们配置apache RewriteRule时出现这种问题,解决办法很简单如下

打开.htaccess 在RewriteRule 后面的index.php教程后面添加一个“?”

完整代码如下

	
  1. .htaccess
  2. RewriteEngine on
  3. RewriteCond $1 !^(index.php|images|robots.txt)
  4. RewriteRule ^(.*)$ /index.php?/$1 [L]

如果是apache服务器出问题,看看是不是的Apache 把 .php 后缀的文件解析哪里有问题了。

总结

Apache 将哪些后缀作为 PHP 解析。例如,让 Apache 把 .php 后缀的文件解析为PHP。可以将任何后缀的文件解析为 PHP,只要在以下语句中加入并用空格分开。这里以添加一个 .phtml 来示例。 
AddType application/x-httpd-php .php .phtml 
为了将 .phps教程作为 PHP 的源文件进行语法高亮显示,还可以加上: 
AddType application/x-httpd-php-source .phps 
用通常的过程启动 Apache(必须完全停止 Apache 再重新启动,而不是用 HUP 或者USR1 信号使 Apache 重新加载)。

(三)nginx配置遭遇No inputfile specified

虚拟机测试nginx 遭遇 Noinput file specified,多方查找终于找到解决办法

1、 php.ini(/etc/php5/cgi/php.ini)的配置中这两项 
cgi.fix_pathinfo=1 (这个是自己添加的) 
doc_root=

2、nginx配置文件/etc/nginx/sites-available/default中注意以下部分

	
  1. location ~ \.php$ {
  2. fastcgi_pass 127.0.0.1:9000;
  3. fastcgi_index index.php;
  4. fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
  5. include fastcgi_params;
  6. }

路径需要根据你主机主目录的实际情况填写

配置完以上部分,重启一下service nginx restart,应该没问题了


3、修改preg_replace为preg_replace_callback函数

因为preg_replace不能用/e修饰符,所以用preg_replace_callback代替preg_replace,修改ThinkTemplate.class就可以了。

其中有Think\Lib\Template\ThinkTemplate.class.php和ThinkPHP\Lib\Core\Dispatcher.class.php两处需要修改

(一)修改Think\Lib\Template\ThinkTemplate.class.php

这一步是为了让模板兼容

请先覆盖Thinkphp论坛解决方案

覆盖完这个文件先别急,这时候运行会发现报错了:

Call to undefined method TagLib::_()

这是因为闭包函数不能直接使用外部变量。

把417行(左右)

	
  1. $content = preg_replace_callback($patterns, function($r) {
  2. return $this->parseXmlTag($tagLib, $tag, $r[1], '');
  3. },$content);

改为:

	
  1. $content = preg_replace_callback($patterns, function($r) use ($tagLib,$tag) {
  2. return $this->parseXmlTag($tagLib, $tag, $r[1], '');
  3. },$content);

把426行(左右)

	
  1. $content = preg_replace_callback($patterns, function($r) {
  2. return $this->parseXmlTag($tagLib, $tag, $r[1], $r[2]);
  3. },$content);

改为:

	
  1. $content = preg_replace_callback($patterns, function($r) use($tagLib,$tag) {
  2. return $this->parseXmlTag($tagLib, $tag, $r[1], $r[2]);
  3. },$content);

(二)修改ThinkPHP\Lib\Core\Dispatcher.class.php

这一步是为了让参数兼容

把134行左右:

	
  1. preg_replace('@(\w+)\/([^\/]+)@e', '$var[\'\\1\']=strip_tags(\'\\2\');', implode('/',$paths));

改为:

	
  1. preg_replace_callback('@(\w+)\/([^\/]+)@',function($r) use (&$var){
  2. $var[$r['1']] = strip_tags($r[2]);
  3. },implode('/',$paths));

附上博主修改好的文件

修改好的文件


备注:用了以下函数、插件的还需要修改

1、修改PPHPExcel

打开PHPExcel\Extend\Vendor\PHPExcel\Calaculation\Functions.php第580行,删掉break;就可以了

2、mysql_get_server_info函数

mysql_get_server_info函数不可用了,暂未找到替代函数

好了,到此就大功告成了!

来源:秋云博客

标签: thinkphp3.1php7

相关文章

Thinkphp6中在构造函数中返回json数据

return json($arr)->send();如果直接在构造函数中使用return json()是不会返回json格式的数据的。必须调用send方法。...

关于thinkphp6 where以数组形式查询,其中有or,and的处理

最近在写tp6的查询语句时,如果查询条件以数组形式传入,比如:$where[] = ['id','=',$id]; $where[] =...

thinkphp项目接入阿里云OSS

1、首先安装 sdk:composer require aliyuncs/oss-sdk-php安装好后,在控制器中创建上传方法:protected function upload...

在thinkphp6.1.1中composer安装 liliuwei/thinkphp-jump报错

报错内容:Problem 1     - Root composer.json requires liliu...

composer安装插件包的时候提示PHP 版本不匹配

因为安装的 PHP 是 8.0 ,不匹配 composer.json 要求的版本,所以使用 composer 进行安装时会报错。在安装命令后加上 --ignore-pla...

phpqrcode防止输出乱码 thinkphp

/** * 封装生成二维码函数 * */ function getQrcode($url){ /*生成二维码*/ vendor("phpqrcod...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。