This question already has answers here:
Symfony3 ClassNotFoundException after bundle creation
(4 answers)
Closed 5 years ago.
I've tried to automatically generate a bundle in Symfony3, using instructions on the official site: https://symfony.com/doc/current/bundles.html, but failed.
I created a directory \src\TestBundle in my project with a file TestBundle.php in it. I extended my class TestBundle from Bundle and added the use Symfony\Component\HttpKernel\Bundle\Bundle; line before the class declaration.
I added my new bundle name to the bundles list in AppKernel.php.
Finally, when I execute $ php bin/console generate:bundle --namespace=TestBundle, I get this error:
PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "TestBundle" from namespace "TestBundle".
Any ideas why that happens?
If you launch this command:
php bin/console generate:bundle --namespace=TestBundle
Symfony create the Bundle for you, you don't need to create file and add It into the AppKernel file.
Try to remove all your files and reference about TestBundle and after launch only the command, Symfony will create and register the Bundle for you.
Instead i you want to create manually thos file you need to create the directory \src\TestBundle and inside It file TestBundle.php.
After you need to register It into your AppKernel.
After you need to tell to composer to load that bundle, if is not already done use this configuration for autoload inside composer.json
"autoload": {
"psr-4": {
"": "src/"
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
},
And finally launch the command inside your console:
composer dump-autoload
Related
Problem
I'm trying to setup a custom directory structure
for some shared classes in my Symfony project. I
want to create a custom folder in the root of my
project and I want to use the Symfony auto-load
feature to automatically register services from
that folder.
So I added a custom services namespace to the
services.yaml file:
# src ./config/services.yaml
services:
...
TestNamespace\:
resource: '../TestNamespace/*'
...
And I added an empty class in the custom folder:
# src ./TestNamespace/TestClass.php
namespace TestNamespace;
class TestClass
{
}
When I run the app I get the following error:
(1/2) InvalidArgumentException
Expected to find class "TestNamespace\TestClass" in file
"/path/to/ClassLoadErrorDemo/demo/TestNamespace/TestClass.php"
while importing services from resource
"../TestNamespace/*", but it was not found! Check the
namespace prefix used with the resource.
(2/2) FileLoaderLoadException
Expected to find class "TestNamespace\TestClass" in file
"/path/to/ClassLoadErrorDemo/demo/TestNamespace/TestClass.php" while
importing services from resource "../TestNamespace/*", but it was not
found! Check the namespace prefix used with the resource in
/path/to/ClassLoadErrorDemo/demo/config/services.yaml (which is loaded
in resource "/path/to/ClassLoadErrorDemo/demo/config/services.yaml").
I double checked the paths, namespace and the class
name multiple times and everything seems fine and I
don't understand why I still get the error.
Controllers in the ./src folder seem to load fine.
What am I doing wrong here?
Steps to reproduce
I created a demo repo to isolate the problem.
git clone https://github.com/smoelker/SymfonyClassLoadErrorDemo.git
cd SymfonyClassLoadErrorDemo/demo
composer install
mv TestNamespace/TestClass.php_ TestNamespace/TestClass.php
php bin/console server:start
Update your composer.json autoload setup
{
[...]
"autoload": {
"psr-4": {
"TestNamespace\\": "TestNamespace/",
"": "src/"
}
},
[...]
}
After run: composer dump-autoload and try again.
composer dump-autoload --classmap-authoritative will only work if your src directory is present at the time you run the command.
This can be an issue with multi-stage Docker builds in particular, when you are normally only copying the composer.json/composer.lock into the build image.
I am making a php application using propel ORM. It gives me the following message when I try to run it:
Fatal error: Uncaught Error: Class 'Propel\Runtime\Propel' not found in C:\MAMP\htdocs\Conference\vendor\bin\generated-conf\config.php:2 Stack trace: #0 C:\MAMP\htdocs\Conference\vendor\bin\list.php(6): require_once() #1 {main} thrown in C:\MAMP\htdocs\Conference\vendor\bin\generated-conf\config.php on line 2.
In my config.php generated file I have this written:
'classname' => '\\Propel\\Runtime\\Connection\\ConnectionWrapper'
What does it all mean? Am I missing some file or what?
I think you are missing a step in the building.
I assume you have your schema.xml file complete and you also have a propel.yaml (or with allowed extension file) properly configured. Also I assume you got Propel with Composer.
If you have all that the next steps are:
1) Open a terminal and go to your project directory, where the schema.xml and propel.yaml files are.
2) Execute the following command to get yout generated-sql (I have to do it this way on Windows):
c:\MAMP\htdocs\Conference\vendor\bin\propel sql:build
3) Get your model classes with the following command:
c:\MAMP\htdocs\Conference\vendor\bin\propel model:build
4) After generating the classes, you have to autoload them. Open your composer.json file with your text editor and add the following:
"autoload": {
"classmap": ["generated-classes/"]
}
It should look like this, for example:
{
"require": {
"twig/twig": "~1.0",
"propel/propel": "~2.0#dev"
},
"autoload": {
"classmap": ["generated-classes/"]
}
}
5) To finish the classes autoloading, you need to execute on your console:
composer dump-autoload
6) And for the runtime connection settings run this for comunicate classes at runtime:
c:\MAMP\htdocs\Conference\vendor\bin\propel config:convert
7) Assuming you have created your database, the last thing you need to do is create the tables, this is with the following command:
c:\MAMP\htdocs\Conference\vendor\bin\propel sql:insert
And there you go! That works for me every time I build a project.
Created the following file:
File: App\Services\Custom\Auth\AuthService.php
Name space: App\Services\Custom\Auth
Class name: AuthCustom
Method inside: foo()
In my controller I'm trying to call the foo method from the Service I created.
App\Services\Custom\Auth\AuthService\AuthCustom::foo()
Why does it keep returning Class 'App\Services\Custom\Auth\Authservice\AuthCustom' not found
What am I doing wrong?
Thanks!!
EDIT:
I added this in the composer.json and run composer dump-autoload without errors.
And it works!
"autoload": {
"classmap": [
"database",
"app/Services/Custom/Auth/AuthService.php"
],
"psr-4": {
"App\\": "app/"
}
},
Your namespace does not match your directory structure. If your class is in App\Services\Custom\Auth\AuthService.php, then your namespace needs to be App\Services\Custom\Auth. If you really want your namespace to be App\Custom\Auth, then your file needs to be App\Custom\Auth\AuthService.php.
Once you fix this, make sure you do a composer dump-autoload on the command line.
It seems that you didn't run composer dump-autoload or php composer.phar dump-autoload.
The composer.json is very important for autoloading!
Laravel needs an big file with all your php files required, usually generated via calling either artisan or composer with : php artisan dump-autoload / composer dump-autoload
It just regenerates the list of all classes that need to be included in the project (autoload_classmap.php). Ideal for when you have a new class inside your project.
More details: http://developed.be/2014/08/29/composer-dump-autoload-laravel/
I have a github repository https://github.com/KoulSlou/UPS and I would like to add it to my project.
In project root I created composer.json file and defined the following autoloading properties:
{
"autoload": {
"files": [
"libraries/Ups/Ups.php",
"libraries/Ups/Ups_Base.php",
"libraries/Ups/Ups_Base_Response.php",
"libraries/Ups/Ups_Live_Rates.php"
]
}
}
When I run
php composer.phar install
repository is being downloaded, but it looks like autoloader is not working. When I try to initialize one of the classes
$test = new Ups()
I got the following error:
Fatal error: Class 'Ups' not found in application/....
Did I define "autoload" property incorrectly?
I'd suggest not using the "files" autoloader, because that isn't very automatic - the files mentioned here are ALWAYS included. Replacing it with "classmap" would be better. And then you'd not be required to mention ALL files, but you can simply state the directory you want to have scanned for classes.
Now what I don't see anywhere: Did you initialize Composer's autoloader anywhere? This usually is something like
require "vendor/autoload.php";
Finaly, I have found out what was the problem. composer.json file in the project I was trying to load - UPS library -was invalid. I was able to download files when I ran:
composer.phar install
but it looks like composer.json file was ignored. I found it out when I ran
composer.phar update
and got
No valid composer.json was found
With option -v I got error that "name" is undefined index. So, I simply added "name" field to the composer.json. Final version is:
{
"name":"KoulSlou/UPS",
"autoload": {
"files": [
"libraries/Ups/Ups.php",
"libraries/Ups/Ups_Base.php",
"libraries/Ups/Ups_Base_Response.php",
"libraries/Ups/Ups_Live_Rates.php"
]
}
}
I follow the docs: http://laravel.com/docs/master/migrations#database-seeding
I placed UserTableSeeder file near DatabaseSeeder. In resources/database/seeds/ folder.
These files are without namespaces (only classes in app/ are namespaced).
Of course there is an exception: exception 'ReflectionException' with message 'Class UserTableSeeder does not exist'
What is the best way to solve this problem?
The default Laravel 5 project has a classmap defined in its composer.json:
{
// ...
"autoload": {
"classmap": [
"database"
],
// ...
}
}
Run composer dump every time you add or remove a class on your database directory to update the Composer autoloader
Reference: https://github.com/laravel/laravel/blob/develop/composer.json
You should use composer dump-autoload command. From the docs:
Once you have written your seeder, you may need to regenerate Composer's autoloader using the dump-autoload command:
composer dump-autoload
Reference here.