.env 파일에 담아둔 값을 service단에서 가져올때
env('env명')을 사용할수 있다
이때 에러가 나서 로그를 찍어보니 값이 안가져왔는데 아래 명령어로 해결되었다
$ php artisan optimize:clear
이 명령어는 캐시를 삭제하는 명령어인데 아래 명령이 모두 실행되는 명령어라고 한다
$ php artisan clear-compiled
# bootstrap/cache/services.php
# bootstrap/cache/packages.php
$ php artisan config:clear
# bootstrap/cache/config.php
$ php artisan route:clear
# bootstrap/cache/routes.php
$ php artisan view:clear
# storage/framework/views
$ php artisan cache:clear
# storage/framework/cache/data
# 2024.02.27 내용 추가
위의 내용을 토대로 만약 env('KEY') 값이 불러와지지 않아 null이라면 optimize clear 처리 되도록 아래 코드를 추가했었다
@YourController or YourService ...
//check
if(is_null(env('KEY'))){
info('env null');
info(env('KEY'));
Artisan::call('optimize:clear');
}
...
여기서 문제가 생긴게 이후 동작은 익셉션 오류가 발생하는 것이였다
재실행시는 문제가 없었으나 한번은 꼭 에러가 나는것을 고쳐야했다
찾아보니 위의 코드는 좋은코드가 아닌 것을 알수있었다
env 값을 불러오기 위해 config/app.php에서 변수 설정을 하고 그 변수값을 가져오는게 베스트인것 같았다
반영하여 수정한 코드는 아래와 같다
@app/config/app.php
<?php
return [
...
/*
|--------------------------------------------------------------------------
| Application Environment
|--------------------------------------------------------------------------
|
| This value determines the "environment" your application is currently
| running in. This may determine how you prefer to configure various
| services the application utilizes. Set this in your ".env" file.
|
*/
'env' => env('APP_ENV', 'production'),
'client_id' => env('EC_CLIENT_ID'),
'client_secret' => env('EC_CLIENT_SECRET'),
...
];
@YourController or YourService ...
$key = Config::get('app.key');
//check
if(is_null($key) || is_null($clientSecret)){
info('env null');
info($key);
}
...
참고자료
https://stackoverflow.com/questions/43243732/laravel-env-always-returns-null
Laravel - env() always returns null
I try to find out why my env() helper always returns null. This causes trouble especially in app.php file, where are env() helpers widely used by default. Perhaps any mysterious server setting? My...
stackoverflow.com
https://qiita.com/mmmmmmanta/items/1df41c4cbc8e77e15326
[Laravel 5.7] キャッシュ系コマンドまとめ - Qiita
artisanコマンドキャッシュ削除php artisan optimize:clearで以下のコマンドが全て実行されます。(パスは初期設定状態で削除されるファイル)php artisan …
qiita.com
https://stackoverflow.com/questions/43243732/laravel-env-always-returns-null
Laravel - env() always returns null
I try to find out why my env() helper always returns null. This causes trouble especially in app.php file, where are env() helpers widely used by default. Perhaps any mysterious server setting? My...
stackoverflow.com
'Laravel' 카테고리의 다른 글
[Laravel] Javascript에서 설정한 쿠키값 laravel에서 사용하기 (0) | 2024.02.21 |
---|---|
[Laravel] 버전 업그레이드 8 → 10 (Windows) (0) | 2024.01.30 |
[Laravel] Validator로 유효성검사 (0) | 2023.08.31 |
[Laravel] Localization 지역화 적용하기 (0) | 2023.08.25 |
[laravel8] 디버그바 설치 (0) | 2023.08.01 |