2015-10-20 39 views
1

我整合了我的ckeditor with ckfinder 3,它有能力在Amazon S3雲上存儲文件。在Laravel應用程序之外訪問.env變量

在我的ckfinder config.php文件中,位於public/assets/plugins/ckfinder我已經硬編碼了與雲建立連接所需的密鑰,密鑰,存儲區和區域。

但我想從.env文件中獲取配置項。

到目前爲止,我想這在ckfinder的config.php文件:

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

但這種失敗的錯誤: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

因此問題是,如何我訪問Laravel應用程序外部環境變量?

謝謝!

回答

1

我也遇到了麻煩。我不確定這是否是最好的方法,但它是有效的。

  1. 需要自動加載磁帶機
  2. 裝入.ENV文件到Dotenv
  3. 參考您的變量

下面是代碼:

// update the paths depending on where the script is 
require_once __DIR__.'/../bootstrap/autoload.php'; 
Dotenv::load(__DIR__.'/..'); 
$dbHost = Dotenv::findEnvironmentVariable('DB_HOST'); 
0
<?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'); } 
?> 
0

我發現這個解決方案與Laravel 5.x與phpdotenv包,insta由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