简体   繁体   中英

Laravel 5 multiple bootstrap/cache/config.php files

Let's say i have one codebase for 500+ clients, based on load balanced web-servers. Each client has their own database, and therefor their own set of credentials.

The easy way here would be to have different .env files with each of the clients credentials and enviornmental settings (this works fine btw), but for optimization reasons, i would like to run the "artisan cache:config" method. This will compile all the config files into a single "config.php" file in /bootstrap/cache".

Now, this is not very functional with lot's of clients. I's it possible to tell Laravel WHERE to look for the cached config file? Without changing core code?

Like defining an enviornmental variable from the vhost "CONFIG_PATH" and (pseudo code)

if( null !== getenv("CONFIG_PATH") && getenv("CONFIG_PATH") != "" ) {
$app->bootstrapConfigPath( getenv("CONFIG_PATH") );
}

Or is the only possibility to have alot of directories, with each their "Bootstrap/cache" folder, and symlinks to the shared codebase ( Which in my opinion is a rather cumberstone path to walk down )

Thanks in advance.

This is possible, but you need to complete a couple of steps. Firstly by extending Illuminate\\Foundation\\Application with your own application class and overriding the bootstrapPath() method. Then you also have to symlink back to the original bootstrap/app.php from your custom bootstrap folder.

Example

Step 1

/app/Applications/MyApp.php - new custom app class

<?php

namespace App\Applications;

use Illuminate\Foundation\Application as Laravel;

class MyApp extends Laravel
{   

  public function bootstrapPath($path = '')
  {

      $bootstrap_env = getenv('APP_BOOTSTRAP');
      if(!$bootstrap_env || !file_exists($bootstrap_env) || !file_exists($bootstrap_env.'/app.php')){
          dd("Install incomplete, please set bootstrap folder via APP_BOOTSTRAP, make sure the folder exists and the app.php is sym linked");
      }
      return $bootstrap_env;
  }

}

Step 2

/app/bootstrap/app.php - update to instantiate new custom class

- $app = new Illuminate\Foundation\Application(
-  realpath(__DIR__.'/../')
- );

+ $app = new App\Applications\MyApp(
+  realpath(__DIR__.'/../')
+ );

Finally

Create custom folders (make sure they are writeable) and a symbolic link of the current app.php to the custom bootstrap location.

mkdir -p /mycustomlocation/bootstrap/cache/

ln -s /laravel/install/bootstrap/app.php /mycustomlocation/bootstrap/app.php

*We couple this with setting a custom .env location using the useEnvironmentPath() method on the instantiated $app also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM