How to pre-generate the configuration cache file?


I would like to use Zend Expressive in a serverless mode (on Lambda for now), so I need to find a way to generate the configuration cache file before uploading my code to AWS, how can I do that?

Hitting any entry point of the framework once will suffice. Loading the index.php via CLI is generally sufficient.

Hum… I see. Thanks for the tip will try it.

I’m wondering if adding a cli console command for that could be a good idea…

Assuming expressive skeleton layout, same configuration for webserver and cli, this should work:

cd path/to/app/root/folder
php -r 'require "vendor/autoload.php"; require "config/config.php";'

However, I imagine you would want to use environment variables managed by lambda to store passwords and such. I would suggest to setup caching config aggregator followed by non-caching aggregator:

// replace return $aggregator->getMergedConfig(); with following:

$noncachedAggregator = new ConfigAggregator([
    // Include cached configuration
    new ArrayProvider($aggregator->getMergedConfig()),
    // include uncached environment configuration
    //   - `env.php`
    //   - `*.env.php`
    new PhpFileProvider(realpath(__DIR__) . '/autoload/{,*.}env.php'),
// null for cache path specifically disables config caching:
], null);

return $noncachedAggregator->getMergedConfig();

Now you can have configuration that will use getenv() while enjoying config caching speedup.

To support local development and deployment without environment variables you will need to gitignore env config files and provide .dist to be copied on build for target infrastructure. Same way as local config files.