Undefined default namespaces in Laravel 5.1 - php

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.

Related

Eclipse PHP spams new warnings since new 2020-06 version

I develop a PHP project in Eclipse. Before updating to last version (2020-06), I used to had no warnings... Since I made this update, it is spamming me with two warnings.
First is about class files, like MyClass defined in MyClass.inc.php : Eclipse says me I must name my file "MyClass.php" or name my class MyClass.inc ... -__________- But I want to continue naming them MyClass.inc.php !!!
Second is about namespaces... I don't use them, and Eclipse says me that << The declared namespace "" does not match the expected namespace "path\folder" >> (path\folder is an exemple for this post).
I use PHP 7.4... I tried filters, but it don't work correctly (may be my bad), and I do not find how to disable this warnings specificaly.
Thanks for helping, I hope some update will fix it it if it's a bug T_T
The new namespace validation rule, although valid, is cumbersome. I guess it's nothing unusual or wrong to have namespaces which don't exactly match the directory structure.
I am working on a mezzio-based application, where this is the usual case, since the framework uses composer for autoloading.
After the upgrade there is no file in my project where Eclipse wouldn't warn that, eg.: The declared namespace "App\Middleware" does not match the expected namespace "project\src\App\src\Middleware".
This warning states the truth but by any means this case should be a reason for a warning in my opinion...
EDIT:
There seems to be an option which allows to change the reported level or to mute the "problem" completely:
Preferences->PHP->Validation->Error/Warnings: Unexpected namespace name
To configure custom paths for namespaces in Eclipse:
Install "PHP Development Tools (PDT) Composer Support"
Right click on the project → Configure → Add Composer Support
A Composer configuration dialog should open
In the "Autoloading" tab, you can assign namespaces to paths (relative to project root)
That would create a composer.json in the root of the PHP project with the following contents:
{
"name" : "my project",
"autoload" : {
"psr-4" : {
"some\\namespace" : "src/some/namespace"
}
}
}
You can define multiple mappings from namespaces to directories. See composer documentation for more information.
For your other problem, I think I would give in and move all files from .inc.php to .php. You'll probably have less problems in the future if you do so.

Undefined class Route in app.blade.php laravel [duplicate]

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.

Is it possible if I change the location of file and namespace will automatic change?

I am currently using Sublime & PhpStorm. I don't have much experience in Sublime, but in PhpStorm if I will create new file in folder, it will automatically create class and namespace of that file. So what if I move file to another folder and it will change namespace according to it.
Is that any feature or package available for this functionality?
I already tried F6 for Reflactor | Move, but maybe I am doing something wrong, here is the screenshots
as you can see, using F6 file is moved but namespace is still same.
PhpStrom has feature to refactor code
Refer: https://www.jetbrains.com/help/phpstorm/refactoring-source-code.html
So once you rename or move class it will update namespace and class both

Symfony Undefined namespace

I am seeing namespace errors all over the place and I am not sure why.
For exampe I have:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
and Configuration is in red and the error reads:
Undefined namespace Configuration less... (Ctrl+F1)
Referenced namespace is not found.
How do I fix this?
Since you say "Configuration is in red" I assume the problem you are experiencing is related to the IDE
If you are using PHPStorm there's a specific Symfony plugin that handle all the namespacing pretty much automatically, as long as your setup is standard.
By standard I mean a project dir containing the app/ src/ web/ ... dirs (if you're using the 2.x Symfony dir layout)
This link will tell you everything you need, in great detail
It may be your .idea folder which is corrupted. Close your project, delete the .idea folder and restart the IDE. It worked for me.

Phalcon php - Undefined namespace Phalcon

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.

Categories