Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

markatice's avatar

Using Session data in a config file

I am using ckfinder/ckfinder-laravel-package, It works well and returns the correct folders when hard coded.

My issue is that I set a Session object using: Session::put('ckfinderCountryLabel', $country->label); in the middleware.

But when I try and access the session data in the config/ckfinder.php config file: $config['resourceTypes'][] = [ 'name' => session('ckfinderCountryLabel'), or 'name' => $_SESSION['ckfinderCountryLabel'], or 'name' => Session::get('ckfinderCountryLabel'), ]; it breaks every time.

How can I use session data in the config file to pass in the country label?

0 likes
1 reply
LaryAI's avatar
Level 58

Accessing session data directly within a configuration file in Laravel is not recommended because configuration files are loaded before the session is available. This means that any attempt to access session data in a config file will fail because the session hasn't been initialized yet.

Instead, you should consider an alternative approach. One common solution is to use a service provider to dynamically modify configuration values after the session is available. Here's how you can do it:

  1. Create a Service Provider:

    First, create a new service provider using the Artisan command:

    php artisan make:provider DynamicConfigServiceProvider
    
  2. Modify the Service Provider:

    In the boot method of your newly created service provider, you can modify the configuration values using session data:

  3. Register the Service Provider:

    Finally, register your service provider in the config/app.php file by adding it to the providers array:

    'providers' => [
        // Other Service Providers
    
        App\Providers\DynamicConfigServiceProvider::class,
    ],
    

By using this approach, you ensure that the session data is available when you modify the configuration, and you avoid trying to access session data directly in the configuration files. This method allows you to dynamically adjust configuration settings based on session data after the session has been initialized.

Please or to participate in this conversation.