Home > Enterprise >  Laravel - Load a JSON file from /resources folder for API
Laravel - Load a JSON file from /resources folder for API

Time:10-15

Within a Laravel project, I have an API that is powered by a flat JSON file. I'd like to use the JSON file where it currently resides, which is inside the /resources/modules/ folder in project. Problem is I'm having trouble targeting this location with the Storage:: method. Currently I have the file located in storage/app/public, which works fine with the code below, but ideally I'd reference the /resources/modules/ file.

    $this->json_path = Storage::disk('public')->get('file.json');
    $this->config_decoded = json_decode($this->json_path, true);

How do I do this using the Storage:: method?

CodePudding user response:

Unless you've updated your public disk to point to the resources/modules directory, this is to be expected. By default, the public disk points to your storage/app/public directory.

You can either set up a new disk in your config/filesystems.php file by adding the something like the following to the disks array:

'modules' => [
    'driver' => 'local',
    'root' => resource_path('modules'),
    'throw' => false,
],

Then your code would be:

$this->json_path = Storage::disk('modules')->get('file.json');
$this->config_decoded = json_decode($this->json_path, true);

Alternatively, you could use the File facade instead:

use Illuminate\Support\Facades\File;

$this->json_path = File::get(resource_path('modules/bob.json'))
  • Related