Skip to content

Instantly share code, notes, and snippets.

@skadimoolam
Created June 30, 2020 08:57
Show Gist options
  • Save skadimoolam/baf2aed01029ab6b763bf5cb9e80deee to your computer and use it in GitHub Desktop.
Save skadimoolam/baf2aed01029ab6b763bf5cb9e80deee to your computer and use it in GitHub Desktop.
How to load custom json files into Laravel's config - Simplest Web | https://simplestweb.in/blog/how-to-load-custom-json-files-into-laravels-config
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
if (file_exists(storage_path('settings.json'))) {
$settings = json_decode(file_get_contents(storage_path('settings.json')), true);
config(['settings' => $settings]);
}
}
}
Route::post('/settings', function (Request $request) {
$rawSettings = json_decode($request->get('settings'), true);
if ($rawSettings == null) abort(500);
$text = json_encode($rawSettings, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
file_put_contents(storage_path('settings.json'), $text);
return redirect('/');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment