Search code examples
phplaraveldeploymentforge

Laravel forge after each deploy having to use composer update


So I have a helper file: App\helpers.php, added in my JSON as follows:

"autoload-dev": {
    "psr-4": {
        "Tests\\": "tests/"
    },
    "files": [
        "app/helpers.php"
    ]
},

In this helper file I have for example a simple userName() method for displaying the full name of a user:

function userName()
{
    return auth()->user() ? auth()->user()->present()->name : '';
}

Everything works fine in my local environment. But everytime I push to my production environment, I get errors:

Call to undefined function userName()

Whenever in Laravel Forge I do a composer update after deploying, the error disappears and my website works flawlessly.

I have never had this issue with any other projects, and I am using the helpers.php file the exact same way.

My question is, why is this happening and of course my second question would be, how to solve this?


Solution

  • if you look at forge deploy script, you'll see the line:

    $FORGE_COMPOSER install --no-dev --no-interaction --prefer-dist --optimize-autoloader
    

    the --no-dev flag will skip your autoload-dev rules, so you just need to move the helper reference into autoload instead of autoload-dev

     "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        },
        "files": [
            "app/Helpers.php"
        ]
    },