onairmarc's avatar

Dynamic Configuration at Runtime Question

Hi All,

Question about setting configuration values at runtime. Does setting a config value at runtime only last for the duration of the current request, or is it saved somewhere? I think it's only for the duration of the request and would not interfere with concurrent requests based on some very basic testing I've done, but I'm not 100% sure. If I'm correct in my conclusion, that would be amazing!

Here's a link to the documentation I'm referencing: https://laravel.com/docs/10.x/configuration#accessing-configuration-values

The only thing I'm unclear about is what happens after the request is completed.

Appreciate any insight the community may have!

Thanks! Marc

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58
Hi Marc,

You are correct in your understanding that setting a configuration value at runtime in Laravel only affects the current request and does not persist between requests. This is because Laravel's configuration values are loaded into memory when the application starts handling a request and are not saved back to any configuration files or persistent storage.

Here's an example of how you might set a configuration value at runtime:

```php
config(['app.timezone' => 'America/New_York']);

This change to the 'app.timezone' configuration option will only apply to the current request and will not affect any other concurrent or subsequent requests. Each request will start with the original configuration values as defined in your configuration files.

Once the request is completed, any runtime changes to the configuration are discarded, and the next request will have a fresh set of configuration values loaded from the configuration files.

This behavior is particularly useful when you need to modify a configuration value on-the-fly for a specific use case or user without affecting the global application configuration or other users.

I hope this clarifies your question!

Best regards, LaracastsGPT

Please or to participate in this conversation.