thinkphp6: 自定义配置文件(php 8.1.1 / thinkphp v6.0.10LTS)
一,创建商品图片所在的目录:
liuhongdi@lhdpc:/etc/nginx/sites-enabled$ cd /var/www/html/ liuhongdi@lhdpc:/var/www/html$ sudo mkdir goodsimage [sudo] liuhongdi 的密码: liuhongdi@lhdpc:/var/www/html$ sudo chmod 777 goodsimage
说明:刘宏缔的架构森林是一个专注架构的博客,
网站:https://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/05/29/thinkphp6-zi-ding-yi-pei-zhi-wen-jian-php-8-1-1-thinkphp-v6/
对应的源码可以访问这里获取: https://github.com/liuhongdi/
或: https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,编写php代码:
1,在.env中增加配置:
[GOODSIMAGE] GOODS_IMAGE_DIR=/var/www/html/goodsImage GOODS_IMAGE_HOST=http://192.168.219.6
2,创建配置文件
在config目录下,创建images.php
内容:
<?php return [ "goodsImageDir"=>env('goodsimage.goods_image_dir' ), "goodsImageHost"=>env('goodsimage.goods_image_host'), ];
3,在controller中读取配置
class Article extends BaseController { /** * 读取配置 * * @return \think\Response */ public function read() { // 返回数组 $images = GConfig::get('images'); // 返回 stmp.qq.com $dir = GConfig::get('images.goodsImageDir'); $host = GConfig::get('images.goodsImageHost'); $image = ["all"=>$images, "dir"=>$dir, "host"=>$host]; //var_dump($image); return Result::Success($image); } }
三,测试效果
访问地址:
http://192.168.219.6:8000/article/read
返回:
四,查看php和thinkphp的版本:
php:
liuhongdi@lhdpc:/data/php/admapi$ php --version PHP 8.1.1 (cli) (built: Dec 20 2021 16:12:16) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.1, Copyright (c) Zend Technologies with Zend OPcache v8.1.1, Copyright (c), by Zend Technologies
thinkphp:
liuhongdi@lhdpc:/var/www/html$ cd /data/php/admapi/ liuhongdi@lhdpc:/data/php/admapi$ php think version v6.0.10LTS