在 Laravel 專案中,我有一個由平面 JSON 檔案提供支持的 API。我想使用它當前所在的 JSON 檔案,該檔案位于專案的 /resources/modules/ 檔案夾中。問題是我無法使用 Storage:: 方法定位此位置。目前我的檔案位于 中storage/app/public,它適用于下面的代碼,但理想情況下我會參考該/resources/modules/檔案。
$this->json_path = Storage::disk('public')->get('file.json');
$this->config_decoded = json_decode($this->json_path, true);
如何使用 Storage:: 方法做到這一點?
uj5u.com熱心網友回復:
除非您已將public磁盤更新為指向該resources/modules目錄,否則這是意料之中的。默認情況下,public磁盤指向您的storage/app/public目錄。
config/filesystems.php您可以通過將以下內容添加到陣列中來在檔案中設定新磁盤disks:
'modules' => [
'driver' => 'local',
'root' => resource_path('modules'),
'throw' => false,
],
那么您的代碼將是:
$this->json_path = Storage::disk('modules')->get('file.json');
$this->config_decoded = json_decode($this->json_path, true);
或者,您可以改用File外觀:
use Illuminate\Support\Facades\File;
$this->json_path = File::get(resource_path('modules/bob.json'))
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/514648.html
上一篇:在LARAVEL中對集合進行分組
