Search code examples
phplaravelenvironment-variablesenvironmentforge

Why re-install repository in forge laravel change data in env?


Before I re-install my repository in the forge laravel, I backup my env file

After I install my repository in the forge laravel, the data in env had changed

There are 3 keys that changed, that are APP_KEY, DB_USERNAME, DB_PASSWORD

What I want to ask is :

Do I keep using APP_KEY, DB_USERNAME, DB_PASSWORD like in my backup env or I use APP_KEY, DB_USERNAME, DB_PASSWORD new?


Solution

  • The composer.json file contains some scripts that are run after installing a project:

        "post-root-package-install": [
          "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
          "php artisan key:generate"
        ],
        "post-install-cmd": [
          "Illuminate\\Foundation\\ComposerScripts::postInstall"
        ],
        "post-update-cmd": [
          "Illuminate\\Foundation\\ComposerScripts::postUpdate"
        ],
    

    If .env does not exist in the project root, it will copy .env.example to .env. After that, php artisan key:generate is run which sets the new APP_KEY.

    The APP_KEY doesn't matter so much when you're dealing with a new project and have no encrypted data. DB_USERNAME and DB_PASSWORD you probably need your old values if you have a database already you want to reconnect to.

    From Laravel Docs:

    Application Key

    The next thing you should do after installing Laravel is set your application key to a random string. If you installed Laravel via Composer or the Laravel installer, this key has already been set for you by the php artisan key:generate command.

    Typically, this string should be 32 characters long. The key can be set in the .env environment file. If you have not renamed the .env.example file to .env, you should do that now. If the application key is not set, your user sessions and other encrypted data will not be secure!