sunrise's avatar

The location of .env file

I need to change the location of .env file,
for example, there is a project named "project-a",
.env file's original location is "/project-a", I want to change it to "../project-a",so,I cut it from original location "/project-a" and paste it in new location "../project-a".

Question: In order to use .env file normally,what setting should I modify?

0 likes
7 replies
MikeHopley's avatar

From auther. phpdotenv is made for development environments, and generally should not be used in production.

That's true; however, bear in mind that recommendation is for performance reasons, which may not be especially relevant to many websites:

In production, the actual environment variables should be set so that there is no overhead of loading the .env file on each request.

That's just one file, while your Laravel project might already be loading ~200 -- 400 files per request. I don't think it makes much difference.

But yes, the absolutely optimal solution is to set the environment variables in your server config.

jlrdw's avatar

In development I do use the dotenv however in production all hard-coded configs like you mentioned above to me that's just a better overall thing to do no chance of someone reading the environment file. A lot of newbies don't know how to properly protect the Environment file as at @Snapey has mentioned before.

1 like
usman's avatar

@sunrise , For your http application environment add the following code inside your app/Http/Kernel.php:

use Illuminate\Routing\Router;
use Illuminate\Contracts\Foundation\Application;
//////
    /**
     * Create a new HTTP kernel instance.
     *
     * @param  \Illuminate\Contracts\Foundation\Application  $app
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    public function __construct(Application $app, Router $router)
    {
        $app->useEnvironmentPath(base_path() . '/../'); // here you can customize the path.
        parent::__construct($app, $router);
    }

For console environment add this code inside app/Console/Kernel.php file:

use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Foundation\Application;

/////
    /**
     * Create a new console kernel instance.
     *
     * @param  \Illuminate\Contracts\Foundation\Application  $app
     * @param  \Illuminate\Contracts\Events\Dispatcher  $events
     * @return void
     */
    public function __construct(Application $app, Dispatcher $events)
    {
        $app->useEnvironmentPath(base_path() . '/../'); //here customize the path
        parent::__construct($app, $events);
    }

Please note the comments. The above code will allow you to move the .env file out of the laravel installation directory (one level up and I think this is what you need. So, no need to change the code).

Usman.

4 likes
vinodmot's avatar

For your http application environment add the following code inside your app/Http/Kernel.php:

use Illuminate\Routing\Router;
use Illuminate\Contracts\Foundation\Application;

    /**
    * Create a new HTTP kernel instance.
    *
    * @param  \Illuminate\Contracts\Foundation\Application  $app
    * @param  \Illuminate\Routing\Router  $router
    * @return void
    */
    public function __construct(Application $app, Router $router)
    {
        $app->useEnvironmentPath(base_path() . '/public/'); // here you can customize the path.
        parent::__construct($app, $router);
    }

.env file cut and paste in your public folder

gmlconslting's avatar

While vinodmot's answer above will work please DO NOT copy and paste this code without configuring the proper .htaccess/nginx rules.

Using:

$app->useEnvironmentPath(base_path() . '/public/');

and placing your .env in the public folder will allow public access to the file contents. There are ways to store it here and prevent outside access but considering you can point the envPath anywhere in the application it is better to store it elsewhere.

I'm sure vinodmot is fully aware of this and just used the public directory as an example, but i wanted to highlight the issue to prevent any absent-minded copy-pasting.

PS: for a true horror-show, google "filetype:env"

fortran's avatar

My solution:

// AppServiceProvider.php

public function boot() {
    // alter .env path
        app()->useEnvironmentPath(base_path('..'));
}

Please or to participate in this conversation.