Hardist
Hardist

Reputation: 1983

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?

Upvotes: 0

Views: 763

Answers (1)

Rob
Rob

Reputation: 220

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"
    ]
},

Upvotes: 2

Related Questions