Created
June 30, 2020 08:57
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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]); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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