Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

php - Can't change configuration value at runtime, change doesn't save. Laravel 8

SettingController:

public function index()
{
    return view('backend.settings.index');
}

public function update(Request $request)
{
    $settings = $request->input('name');
    config(['settings.site_name' => $settings]);

    return redirect()->route('settings');
}

view:

<form action="{{ route('settings.update') }}" method="POST">
@csrf
    <div class="form-group">
        <label for="name">Site name</label>
        <input type="text" name="name" value="{{ config('settings.site_name') }}" id="name" required>
    </div>
    <div class="form-group">
        <input type="submit" class="paper-btn btn-secondary" value="Update">
    </div>
</form>

Variable $settings gets the correct data from input, I checked that with dd(), but change doesn't save after redirect. What did I do wrong?

question from:https://stackoverflow.com/questions/65901149/cant-change-configuration-value-at-runtime-change-doesnt-save-laravel-8

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your run time configuration settings like that won't persist as that Config Repository is only loading the config into memory and doesn't deal with persistence, so you are only setting an element in memory when you set a config value. That memory is gone after the request is finished; every request is starting a new process that is booting the framework fresh and loading the configuration.

You should probably look into storing your settings in the database and caching them. Then you could retrieve them from your persistent storage and then set those configuration values based on what you pulled for your settings (at run time).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...