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.
Related
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.
I have learned that in best case a namespace should represent the path of its file. That means if the file is into the subdirectory "models", like in this case
vehicles
cars
models
the namespace should be: namespace vehicles\cars\models;
Now, I have an example from Symfony 3 that looks like this:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
I would like to understand where those namespaces are located in. "Symfony" and "Sensio" I can both find in the directory "vendor". For example if I follow the path "Symfony", I don't find "Bundle" or if I follow "Sensio", there is also no folder called "Bundle".
Is that because Symfony doesn't care about the standards or am I just not understanding how it works? I hope it's the second one.
I hope someone can explain that to me, so I can understand.
Symfony has a more complex file structure the namespace Symfony corresponds to the symfony/symfony/src/Symfony folder so if you are looking for Symfony\Bundle\FrameworkBundle\Controller\Controller just go there and then you will find Bundle/FrameworkBundle.
On the other hand, for most vendor bundles the namespace corresponds to the exact folder structure like for example Sensio\Bundle\FrameworkExtraBundle\Configuration\Route
If you really can't find the path to a vendor bundle you can always try looking into vendor/composer/autoload_namespaces.php and autoload_psr4.php.This is where composer defines the correspondences between directories and namespaces for your project
Hope that helps
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 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.
Instead of Using composer I download zip file of a bundle and extract this bundle to my symfony project. I change all relative address for example I change all namespace Trsteel\CkeditorBundle; to namespace Acme\TrsteelCkeditorBundle; for doing this I searched the downloaded directory for every Trsteel\CkeditorBundle and change all of them to Acme\TrsteelCkeditorBundle now when I want to use this Bundle on my project I register this bundle to my appKernel.php of symfony project But I reached this error
Error: Class 'Acme\TrsteelCkeditorBundle\TrsteelCkeditorBundle' not found in /Users/kingkong/Documents/workspace/dev/Project/SRC/app/AppKernel.php line 45
File names should be the same as namespace structure, capitalization may be an issue, so check that.
You may want to place the bundle in this way - "Acme\Trsteel\CkeditorBundle\CkeditorBundle"
Check whether you have actually changed the folder structure from Trsteel\CkeditorBundle to Acme\TrsteelCkeditorBundle. And also check your routing files as well. (I assume you have done the routing part as the error won't say that it can't get to that location.)
Cheers!