Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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