tp5 与 php版本兼容,5.1对比5.0.X版本需要注意点

# 升级说明

>

> + [5.0.X升级到5.1官方说明](https://www.kancloud.cn/manual/thinkphp5_1/354155)

>+ [5.0.X升级到5.1官方说明2](http://www.thinkphp.cn/topic/47882.html)

补充说明

* 获取数据表信息

\think\Db::getTableinfo('表全名');

## 添加修改post

```

//自动加入字段编辑

protected function changeData()

{

if ($this->request->isPost()){

$post = input('post.');

$post['test_time'] = strtotime(input('post.test_time'));

$post['finish_time'] = strtotime(input('post.finish_time'));

$post['charge_operator_time'] = strtotime(input('post.charge_operator_time'));

$post['charge_customer_time'] = strtotime(input('post.charge_customer_time'));

$this->request->withPost($post);

}

}

```

## 1.入口文件 index.php

>[info]如下,与之前版本有所区别

~~~

namespace think;

// [ 应用入口文件 ]

// 定义应用目录

// supper tp5.1

if (!defined('DS')) {

define('DS', DIRECTORY_SEPARATOR);

}

define('APP_PATH', __DIR__ . './apps/');

// 加载框架引导文件

require __DIR__ . './thinkphp/base.php';

// 执行应用并响应

Container::get('app', [APP_PATH])->run()->send();

~~~

## 2.配置区别

>[info]所有配置 位于项目

>config目录下,并区分

>[warning] 注意: 模块配置文件,集中存放在 项目 根目录下的 **config** 之中,如下目录结构

~~~~

www WEB部署目录(或者子目录)

├─application 应用目录

│ ├─common 公共模块目录(可以更改)

│ ├─module_name 模块目录

│ │ ├─common.php 模块函数文件

│ │ ├─controller 控制器目录

│ │ ├─model 模型目录

│ │ ├─view 视图目录

│ │ ├─config 配置目录

│ │ └─ ... 更多类库目录

│ │

│ ├─command.php 命令行定义文件

│ ├─common.php 公共函数文件

│ └─tags.php 应用行为扩展定义文件

├─config 应用配置目录

│ ├─module_name _模块配置目录_

│ │ ├─database.php 数据库配置

│ │ ├─cache 缓存配置

│ │ └─ ...

~~~~

## 3.控制器 controller 注意点

### 3.1初始化方法区别

默认初始化方法名称变更

5.0.x版本为 **_initialize()** 而5.1为 **initialize()**

>[warning] 注意 少了 **_**

5.1版本 代码如下

~~~

namespace app\index\controller;

use think\Controller;

class Index extends Controller

{

public function initialize()

{

echo 'init
';

}

-----

}

~~~

### 3.2模板输出区别【默认进行html过滤】

>[info]模板输出html代码需要特别注意,默认系统对html进行了过滤

~~~

.....

class Index extends Controller

{

public function index()

{

$html = '

hi

';

$this->assgin('html',$html);

return $this->fetch();

}

.....

}

要输出完整的html 在对应模板 index.html 中

{$hmtl |raw}

~~~

>[warning] 调用函数 raw 进行处理,

## 4.原系统常量问题

>[info]5.1版本后,取消了系统常量

* 解决办法一

> 参考之前版本,自定义

define('XXX','sdfsdf');

* 解决方法二

> 采用tp5.1 版本方法

> 如获取系统版本信息,

` app()->version() ` 注:之前是 `THINK_VERSION`

Env::get(); 或 app('env')->get()

## 5.系统类实例化区别

> 实例化缓存类方法

* 方法一 类以于以前版本

~~~

use think\facade\Cache;

$cache = new Cache();

~~~

* 方法二 新方法

~~~

注册容器对象实例

$app = app();

// 判断对象实例是否存在

isset($app->cache);

$app->cache = think\Cache::class;

// 获取容器中的对象实例

$cache = $app->cache;

// 快速调用(自动实例化)

$cache = app('cache');

// 每次调用都会重新实例化

$cache = app('cache',true);

~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值