2

I integrated my ckeditor with ckfinder 3 that has the ability to store files on the Amazon S3 cloud.

In my ckfinder config.php file, that is located in public/assets/plugins/ckfinder i have hardcoded the key, secret key, bucket and region, that are needed to make connection with the cloud.

But i want to get the config items from the .env file.

So far i tried this in ckfinder's config.php:

require $_SERVER['DOCUMENT_ROOT'].'/../bootstrap/autoload.php';
$app = require $_SERVER['DOCUMENT_ROOT'].'/../bootstrap/app.php';
var_dump($app->environment('S3_KEY');

But this fails with an error: Fatal error: Uncaught exception 'ReflectionException' with message 'Class env does not exist' in /home/vagrant/projects/dk/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 779

So the question is, how to i access environment variables outside Laravel app ?

Thank you!

musicvicious
  • 1,043
  • 16
  • 21

3 Answers3

3

I found this solution with Laravel 5.x with phpdotenv package, installed by composer require vlucas/phpdotenv:

require __DIR__.'/../vendor/autoload.php'; //Load composer autoload
$dot = new \Dotenv\Dotenv(__DIR__.'/../'); //Location of .env
$dot->load(); //Load the configuration (Not override, for override use overload() method
$url = getenv('APP_URL'); // Get var value
Gally
  • 73
  • 5
2

I had trouble doing this as well. I'm not sure if this is the best way to do it, but it works.

  1. Require the autoloader
  2. Load the .env file into Dotenv
  3. Reference your variable

Here is the code:

// update the paths depending on where the script is
require_once __DIR__.'/../bootstrap/autoload.php'; 
Dotenv::load(__DIR__.'/..');
$dbHost = Dotenv::findEnvironmentVariable('DB_HOST');
ajon
  • 7,868
  • 11
  • 48
  • 86
1
<?php
$_ENV = array();
$handle = fopen(".env", "r");
if($handle) {
    while (($line = fgets($handle)) !== false) {
      if( strpos($line,"=") !== false) {
        $var = explode("=",$line);
        $_ENV[$var[0]] = trim($var[1]);
        }
    }
    fclose($handle);
} else { die('error opening .env'); }
?>