Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

enadabuzaid's avatar

when use forge via cli get error in ForgeServiceProvider

when using any command for forge`` I get this error forge server:list```

In ForgeServiceProvider.php line 39:

Undefined variable $tokens

0 likes
5 replies
LaryAI's avatar
Level 58

It looks like you're encountering an issue with an undefined variable $tokens in the ForgeServiceProvider.php file when running Forge CLI commands. This typically happens when the service provider is trying to access a variable that hasn't been properly initialized or passed.

Here's a step-by-step solution to resolve this issue:

  1. Check the ForgeServiceProvider.php file: Open the ForgeServiceProvider.php file and locate the line where $tokens is being used. Ensure that $tokens is properly defined and initialized before it is used.

  2. Initialize the $tokens variable: If $tokens is not initialized, you need to initialize it. This might involve fetching tokens from a configuration file or an environment variable.

Here is an example of how you might initialize the $tokens variable:

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class ForgeServiceProvider extends ServiceProvider
{
    public function register()
    {
        // Ensure that $tokens is initialized
        $tokens = config('forge.tokens', []);

        // Your existing code that uses $tokens
        // For example:
        if (empty($tokens)) {
            throw new \Exception('No Forge tokens found in configuration.');
        }

        // Register the Forge service with the tokens
        $this->app->singleton(Forge::class, function ($app) use ($tokens) {
            return new Forge($tokens);
        });
    }

    public function boot()
    {
        // Any boot logic if necessary
    }
}
  1. Check your configuration file: Ensure that your configuration file (e.g., config/forge.php) contains the necessary tokens. It might look something like this:
return [
    'tokens' => [
        'your-token-1',
        'your-token-2',
        // Add more tokens as needed
    ],
];
  1. Environment variables: If you are using environment variables to store your tokens, make sure they are properly set in your .env file and that your configuration file is correctly fetching them:
// In your .env file
FORGE_TOKENS=your-token-1,your-token-2

// In your config/forge.php file
return [
    'tokens' => explode(',', env('FORGE_TOKENS', '')),
];

By ensuring that the $tokens variable is properly initialized and available in the ForgeServiceProvider, you should be able to resolve the "Undefined variable $tokens" error.

If you continue to experience issues, please provide more details about the specific code in your ForgeServiceProvider.php file, and I can offer more targeted assistance.

floryan's avatar

I'm having the same problem when I try to run any command with Forge CLI.

uruloke's avatar

Could you share the full stacktrace by running one of the commands that gives this error with verbose on? --verbose

ChooseMyCarl's avatar

Try using which forge to see if you're still using your previously configured version. For me this has updated to /Users/MYUSER/Library/Application Support/Herd/bin//forge which wasn't expected. It looks like the update that introduces Herd integration with Forge is the cause. I've tried connecting Herd to forge in the UI and it fails.

Not yet solved this problem myself, but hoping to switch back the version of Forge I installed, and was working.

ChooseMyCarl's avatar

Original install and functioning version is available at ~/.composer/vendor/bin/forge for me

Please or to participate in this conversation.