laravel:从配置文件中读取值(10.27.0)
一,添加配置数据
1,在.env中添加:
FILE_HOST="https://file.lhd.com"
FILE_BASE_DIR="/web/site/file/html/image"
2,在config目录下新建文件:
image.php,内容:
<?php
return [
'default_size'=>500,
'link'=>['host'=>'127.0.0.1',
'base'=>'imagebank',
'port'=>3306
],
];
二,php代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\App; class NewsController extends Controller { public function homejson(){ //得到当前是哪一个环境? $environment = App::environment(); //得到环境变量 FILE_HOST $fileHost = env( 'FILE_HOST' , '' ); //得到config中的变量值: $size = config( "image.default_size" ); $link = config( "image.link" ); //返回 return [ 'host' => $fileHost , 'env' => $environment , 'size' => $size , 'link' => $link , ]; } } |
三,测试效果:
说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/10/16/laravel-cong-pei-zhi-wen-jian-zhong-du-qu-zhi-10-27/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com
四,查看laravel框架的版本:
liuhongdi@lhdpc:/data/laravel/dignews$ php artisan --version
Laravel Framework 10.27.0