How can I fix this alert in PhpStorm?
And also how to get autocompletion for Route and other Facades?
Check the Laracasts walkthrough
1) Go to: https://github.com/barryvdh/laravel-ide-helper
2) Click on the gist
Generated version for L5: https://gist.github.com/barryvdh/5227822
3) Click on "Raw" to get the current version (as of June 22, 2016 it is):
https://gist.githubusercontent.com/barryvdh/5227822/raw/4d4b0ca26055fa4753b38edeb94fad2396c497c0/_ide_helper.php
4) Make sure you're in your root directory (this is mine)
cd /var/www/html/project
5) Download the gist:
wget https://gist.githubusercontent.com/barryvdh/5227822/raw/4d4b0ca26055fa4753b38edeb94fad2396c497c0/_ide_helper.php
6) Add "_ide_helper.php" to the .gitignore file
sudo nano .gitignore
Add "_ide_helper.php" on a new line at the bottom
7) Download the .gitignore and _ide_helper.php files into PhpStorm
This is what my PhpStorm project directory looks like afterwards:
8) After the files are downloaded into PhpStorm, the "Undefined class" error will disappear and autocompletion will now work. Might need to restart it.
There is greate IDE support for Laravel shipped from Baryvdh:
https://github.com/barryvdh/laravel-ide-helper
after you install it you just call in the console:
php artisan ide-helper:generate
which generate alll facede shortcuts in _ide_helper.php file (which you have to exclude from git)
There is also something special for PhpStorm:
php artisan ide-helper:meta
which will give Laravel container context for example:
$foo = app(Foo::class);
PhpStorm will know that the $foo variable is type of Foo class.
I know this is an old thread, but it is still relevant. For me, I decided to add the following to my routes/web.php since I don't want to have to worry about regenerating meta data for the app.
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
Edit: The above does not add any overhead to PHP as the use simply acts as a reference for PHP.
I solved this problem simply by installing the Laravel idea plugin.
Use the following link:
https://laravel-idea.com/
You can install it on your phpstorm.
Its good feature is that you can use it for 30 days for free and pay the amount after satisfaction.
Related
I'm developing an app in Laravel 5.1. And for some reason there's loads of default namespace paths that are red (using PHPStorm 10). They are marked as "undefined namespace"
For example, when I make a model with artisan it ends up with this:
use Illuminate\Database\Eloquent\Model;
class Form extends Model
{
//
}
And Eloquent is red. So I can't use the model.
Same goes in app/config/app.php
Where Illuminate\Support\Facades\
where Facades is red.
These are tons of namespace errors like this (under the Illuminate root ). Any idea with it might be? See below...
It's a problem of PHPSTORM.
For those who have this problem, I resolve it by :
- close phpstorm
- remove the project folder .idea
- create a new project with existing files.
It worked for me.
In PhpStorm :
Delete the .idea folder from your project,
File => Invalidate Caches / Restart,
Restart PhpStorm.
It worked for me.
Try this:
Open setting-> (setting path in image)
check "Synchronize IDE settings with composer.json"
When it happens to me, I place my cursor before use and press enter.
Sometimes it fixes that.
I've installed the symfony plugin to PHPstorm, but when I'm trying to run the project I get the following error
Fatal error: Class 'Doctrine\Tests\Common\Cache\CacheTest' not found in ...
I've checked the path and CacheTest file is in the correct folder. When I run the symfony project from the command line it works just fine.
Is there someway to fix this problem?
What you try is not correct. You execute the php.exe and that is the normal PHP executable. To run your Unit-Tests you need PHPUnit to run.
Go to Preferences -> Languages & Frameworks -> PHP -> PHPUnit and define the phpunit.phar.
When you've done that you create a new configuration to run PHPUnit and you define the configuration which is under app/phpunit.xml.
I figured it out. First of all you need add phpunit.phar as Stony explained in his answer.
After that all I really needed to do was to rename phpunit.xml.dist to phpunit.xml in app/ folder and then just right click it and choose the Run 'phpunit.xml' option. This way the Tests succeed and it doesn't give any errors.
As for running the symfony project you need to add symfony command line tool support to phpstorm. All about that in this link: https://confluence.jetbrains.com/display/PhpStorm/Symfony2+Command+Line+Tool+Integration+-+Symfony+Development+using+PhpStorm
After that you just run it as a command like you would with normal command line. That's also explained in the link.
Try to choose "Use custom autoloader" option in Preferences -> Languages & Frameworks -> PHP -> PHPUnit and specify the path to vendor/autoload.php file.
I am getting an error message in my Laravel 5 application:
PHP Fatal error: Cannot redeclare class Illuminate\\Contracts\\Support\\Arrayable in /var/www/.../bootstrap/cache/compiled.php on line 156
This error only occurs on my staging environment and not on my local website. I would love to post more info but I do not have it. Because I do not know where this error is caused.
If I remove the file (cache/compiled.php) everything seems to work fine. But after every deploy the same error occurs. On my localhost everything works fine as well.
My question: does anybody have a clue where to look because I am out of ideas.
Try this way.
At first remove the cache/compiled.php file
then run this command
php artisan clear-compiled
I had the same problem and found the following article, which was very helpful:
https://sentinelstand.com/article/laravel-5-optimization-commands
The only solution that was working for me was manually deleting bootstrap/cache/compiled.php. Switching around the order in which the autoloaders are called in bootstrap/autoload.php did not work for me because I had the same problem the other way round, ie. I had a class in compiled.php that was causing something from autoload.php to be autoloaded before autoload.php ran.
In my case, I am using a combination of PSR4 and manual class mappings in my composer.json file. I'm sure this is part of the problem. (Don't judge me: this app started in Laravel 3, so it's taking time to add namespacing throughout the code base :-).
One reason why things may work differently in different environments is because the artisan optimize command will only generate the bootstrap/cache/compiled.php file if you provide the --force option or if debugging mode is not enabled. So it is likely that you are not getting this file in development because debugging is enabled but are getting this file in staging and/or production because debugging is not enabled.
Ultimately, here's what I have landed on as a solution for production deployments:
artisan config:cache
artisan optimize
rm bootstrap/cache/compiled
Update symlink to point to new version.
This way you still get bootstrap/cache/services.json, which is helpful, whereas artisan clear-compiled removes that file. Also, there will be a very brief period of time where bootstrap/cache/compiled.php will exist, which is why it is important to run these commands before you update the symlink to point your web server at the new version.
It is also worth noting that the compiled.php file that is created by artisan optimize in Laravel 5.1 is no longer generated in Laravel 5.4 because, as Taylor has stated, PHP 7 is much more performant and so the benefit of bundling all the application classes into one file, which is meant to save on disk I/O, is negligble now. Taylor recommends enabling and properly configuring your OPcache instead - you will get far more performance benefits from that.
I experienced the same but eventually found the solution.
I have my own helpers.php files in laravel. Just like the framework ones, I added them as autoloads in my composer.json. A couple of those functions are macros for the Collection (\Illuminate\Support\Collection::macro(...)).
When that helpers.php file is autoloaded, the definition of those macros cause the autoloading of Illuminate\Support\Collection. This in turn uses Illuminate\Contracts\Support\Arrayable.
So basically all of these are already loaded by the time they are defined again in cache/compiled.php. Boom.
Long story short: For me the fix was simply to switch inclusion of the compiled file and the autoloader around.
bootstrap/autoload.php:
$compiledPath = __DIR__.'/cache/compiled.php';
if (file_exists($compiledPath)) {
require $compiledPath;
}
require __DIR__.'/../vendor/autoload.php';
This might not be a viable solution add include code in your compiled file runs right away and refers to helper functions, but I think the chances on that happening should be pretty minimal.
The problem is with the artisan optimize command. If you remove the compiled.php file and then do not run optimize, it should work.
I am working on a project in php Phalcon.
I am working with the Xampp server. The installation process that I followed is on this link:
http://docs.phalconphp.com/en/latest/reference/install.html
I am following the documentation on the website as guide and tutorial.
My problem is, every time I type "class MyClass extends \Phalcon\Mvc\Model"
it says undefined namespace "Mvc" and in other files it also gives error that Phalcon is an undefined namespace.
The installation seems okay.
Please help me out.
I think this is not really a problem with the installation of Phalcon.
I have recognized the same behaviour of my IDE because Phalcon is only loaded as an extension of PHP and not available as real PHP files which can be indexed by your IDE.
The easiest way to check if install was ok is to create a info.php in your project root.
<?php
phpinfo();
If you open this file in browser it should show a block with information about phalcon.
There is a script that creates stub files for you that can be integrated in you project.
https://github.com/phalcon/phalcon-devtools/blob/master/ide/gen-stubs.php
If these files are indexed by your IDE, the message of Undefined namespace should be gone.
If your code works. And IDE shows Mvc undefined namespace then you have to install Phalcon developer tool plugin for your IDE. the plugin is available for PHPStorm. Phalcon is a C extention and for this if you not install developer tool plugin then IDE will show undefined namespace.
Phalcon has different ways to use model like register model class, register namespace or register dir. If you create the model by command rather than manually write code you will get the proper base model class to extend. If still problem then there is problem with Phalcon installation.
Use --namespace="" for model:
phalcon create-model profiles --namespace="Application\Models"
For scaffold:
phalcon create-scaffold profiles --ns-models="Application\Models" --ns-controllers="Application\Controllers"
If you do not register the model namespace and register only model directory then edit the model file after creation and remove the namespace definition.
first check if php load phalcon module. for that in terminal type
php -r "echo phpinfo();" | grep 'phalcon'
if everything went write you should see line
phalcon => enabled
in windows grep wont work and you should look for that line yourself.
I searched a lot, checked many times the neccesary steps, but it still don't work.
My environment is :
Latest eclipse for PHP developer package
Project is newed, not check out directly from SVN
.project has a line
<nature>org.eclipse.php.core.PHPNature</nature>
.buildpath has a line
<buildpathentry kind="con" path="org.eclipse.php.core.LANGUAGE"/>
.settings/org.eclipse.core.resources.prefs
has a line
include_path=0;/projectname
file ensured opened in PHP Editor
Something works:
PHP core function defination displayed when mouse over, and Ctrl+Click will open its defined file, but input 'arr' nothing happens, while a list of array functions expected
Autocomplete does work for internal functios/variables such as class members
But never works for PHP core functions, neither syntax highlighting
Right-Click on your project -> Configure -> Add PHP Support...
Works for me on Eclipse 3.5 / PDT 2.1
For me, nothing worked, except:
Backup and then delete the .db files under: /.metadata/.plugins/org.eclipse.dltk.core.index.sql.h2/
Restart eclipse.
I got this tip from #4 in: http://www.nwiresoftware.com/blogs/nwire/2010/09/five-tips-speeding-eclipse-pdt-and-nwire
As noted before the 'natures' node for php core is listed but this is most insufficient when the project is imported or checked out.
Make a backup of your .project
Remove the 'natures' node children
Close/Re-open eclipse
Right-click project -> Configure -> Add PHP Support
Add back other 'natures' children
The issue is that project will not make the necessary imports because it assumes it already has.
Nothing helped, but this:
In Eclipse:
Go to Window -> Preferences -> General -> Editors -> File Associations
Click Add
Type *.php
Under Associated Editors choose PHP Editor and click Default on the right.
Close and open your php file.
There are 3 .dll files: model.h2.db, model.lock.db and model.trace.db under workspace/.metadata/.plugins/org.eclipse.dltk.core.index.sql.h2
Remove these files and restart Eclipse.
There seem to still be a lot of bugs related to auto-completion for the PHP Development Tools Project (PDT) (See Open Bugs).
One has been entered two days ago: bug 286733 "When using sub namespace completion does not work."
But this is hardly the only one:
"Autocomplete on namespaces failed" (bug 281766)
"[php 5.3] Code assist doesn't provide method completion" (bug 286068)
...
They are all related to the namespace of the PHP code. Could you check if you reproduce the error in a global namespace?
If issue is still exists, follow the following steps:
Remove project from eclipse.
Goto your project folder and remove .Settings folder and .project
file.
Again import php project with different name.
it's work for me.
Right-Click on your project -> Configure -> Convert to PHP Project...
Works for me on Eclipse 4.6 / PDT 5.2