laravel 5 ide autocompletion

2020-07-09 08:46发布

问题:

I want to switch to laravel 5, but have some trouble with ide - autocompletion. I'm using phpstorm. In google, the answers always end up with suggesting to use https://github.com/barryvdh/laravel-ide-helper . But it seems like it is broken for Laravel 5.

The steps I am doing are:

  1. Install Laravel 5

    composer create-project laravel/laravel 
    
  2. Require ide-helper

    composer require barryvdh/laravel-ide-helper
    
  3. Added 'Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider',

    ....
    'Illuminate\Translation\TranslationServiceProvider',
    'Illuminate\Validation\ValidationServiceProvider',
    'Illuminate\View\ViewServiceProvider',
    'Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider',
    
  4. Trying to generate the helper file

    artisan ide-helper:generate
    

But it always breaks with following error:

exception 'InvalidArgumentException' with message 'There are no commands defined in the "ide-helper" namespace.' in C:\xampp\htdocs\test\vendor\symfony\console\Symfony\Component\Console\Application.php:501

0 C:\xampp\htdocs\test\vendor\symfony\console\Symfony\Component\Console\Application.php(535): Symfony\Component\Console\Application->findNamespace('ide-helper')
1 C:\xampp\htdocs\test\vendor\symfony\console\Symfony\Component\Console

\Application.php(192): Symfony\Component\Console\Application->find('ide-helper:gene...')
2 C:\xampp\htdocs\test\vendor\symfony\console\Symfony\Component\Console\Application.php(126): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Outpu
t\ConsoleOutput))
3 C:\xampp\htdocs\test\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php(91): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Out
put\ConsoleOutput))
4 C:\xampp\htdocs\test\artisan(36): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
5 {main}

Maybe someone had the same issue and can help me. I'm open for different solutions for autocompletion other than barryvdh's ide-helper.

回答1:

I had the same problem and this fixed it:

Before you run php artisan ide-helper:generate command, make sure to php artisan clear-compiled and php artisan optimize as it's noted here. If this didn't fixed your problem, take a look at this and clean out PhpStorm cache by choosing this:

File | Invalidate Caches/Restart

After auto restarting, PhpStorm will index again and everything should work fine.

Sorry for my bad english.

Edited: After these steps import your Facades like this use Illuminate\Support\Facades\Auth link them inside your _ide_helper.php file like use Auth.



回答2:

I ran into the same problem. These are the steps I took to fix it:

  • I double-checked that 'Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider' was correctly added to the providers array in config/app.php.
  • I executed artisan clear-compiled. Had no effect

Executing php artisan config:clear fixed the problem.



回答3:

This error occur when the package ServiceProvider isn't loaded.

If you have multiple config file (like for different environment), you must ensure that the service provider is well set in all the environment you which to use the package.

config/
  local/
    app.php
  app.php

to verify if the service provider is correctly set to your application you can dump the app config:

dd(\Config::get('app.providers'));


回答4:

Try this:

php artisan ide-helper:generate


回答5:

Here is an updated gist as of this month. I have tested this and it works in PHPStorm.

Also you don't have to install this through composer. Copy the gist and save it in your root folder as _ide_helper.php.