Sunday, January 9, 2022

[FIXED] Yii 1 Console Application Environment Variables

Issue

For a Yii 1 web application, I am using the symfony/dotenv library to read and load environment variables from a .env file. To do this, I added a code in the index.php file,

require 'vendor/autoload.php'; //autoload for composer

if(file_exists('/path/to/.env')){
    $dotenv = new Symfony\Component\Dotenv\Dotenv();
    $dotenv->load(__DIR__.'/path/to/.env');
}
else{
   // Missing .env file
   exit;
}

This works well with the web application. However, for Yii console applications, this does not work because index.php is not being loaded. Can this be done inside the console.php file? How?


Solution

For console application you can do the same in protected/yiic.php. This file is used for bootstrap when you call ./yiic, in similar way as index.php is loaded on web request.



Answered By - rob006

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.