Search code examples
laravelphpdotenv

How to access settings from `.env` inside the `index.php`


I’m creating a deployment configuration for Laravel project. On my hosting public/ folder must be moved to another place. Obviously, since that, I need to change path to the autoload.php and app.php in index.php.

However, I’d like to add and use a parameter which would tell where these files reside. Something like this:

require __DIR__ . '/../' . env('DEP_EXT_FOLDER') . 'vendor/autoload.php';

I think, the most proper place for such a parameter is .env file. However, I’ve got an error:

Call to undefined function env() 

Solution

  • You can't use the env() function before loading the autoloader.

    if you absolutely want to use your .env file, you will have to use native php preg_match() for finding your key and using the value after that :)