In a project (on which work 2), we are forced to make a "php composer.phar dump-autoload" and "php composer.phar dump-autoload -o" every time we add a new file (a new class in our web project).
If we do not type these lines of command, the autoloader dial tells us that this class is not found ...
Whereas with personal projects, I have never been compelled to type these command lines for each new class of creation.
In my composer.json I have put this:
"autoload": {
"psr-4" : {
"App\\": "app/",
"Database\\": "app/",
"Helpers\\": "app/",
"": [
"app/",
"lib/"
]
}
}
Thus I does not understand, why, for every time we add a file in the folder " app ", we have to make "php composer.phar dump-autoload" and "php composer.phar dump-autoload -o" ...
ps : I have Classes without Namespace in directories "app" and "lib".
Will you have an idea of where it could come please?
Thank you.
The only reason for having to dump the autoloader after adding a new class is when you are using classmap autoloading, and no PSR-0 or PSR-4.
This should be clearly indicated in the composer.json file of that application, which would be something like "autoload":{"classmap":[...]}.
If this is NOT present, there would be no requirement to dump the autoloader.
If however composer dump-autoload does not really work, and composer dump-autoload -o is needed, you have errors in your PSR definition of autoloading.
I'm confused whether your excerpt from your composer.json is from your nonworking example or not - all I can say is that it is supposed to work without dumping. It still is not optimal, though.
Related
I'm trying to get started with nwidarts modular laravel architecture. I just got done installing it according to it's site: https://nwidart.com/laravel-modules/v2/basic-usage/creating-a-module
Now when I run the command php artisan module:make <module-name> I get the error:
[Symfony\Component\Console\Exception\CommandNotFoundException]
There are no commands defined in the "module" namespace.
I am running this in the root folder of my laravel app. So I am not entirely sure I installed it in the right place but there is an nwidart folder and stuff in my apps vendor folder so I guess I did it right? What did I do wrong?
follow the instructions on command
step 1: composer require nwidart/laravel-modules
step 2: php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider"
step 3: insert in composer.json
{
"autoload": {
"psr-4": {
"App\\": "app/",
"Modules\\": "Modules/"
}
}
}
step 4: composer dump-autoload
I decided to run "composer update" command another time to get the app back up to date but now it's completely broken as it can't find a certain class anymore.
Fatal error: Class 'App\Application' not found in /home/rlvpr/public_html/webroot/index.php on line 33
All files in the vendor folder have been properly uploaded, so yes also all the composer autoload class mappings ...
I really have no clue what I should do now :/
Anyone got any suggestions?
Thanks!
Try running composer dumpautoload.
If that doesn't work, check your composer.json for the autoload section:
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
Then dump autoload again. Of course, tweak the source folder path to suit where your namespace folder is!
Run:
composer dump-autoload
See also dump-autoload
Very recently, my application stopped working properly for some reason and I had to change my application namespace, so it is projectname\Model instead of App\Model.
Now after this change, everything started working normally, except php artisan commands.
When I call php artisan, I can have the lists of artisan commands but none works, I always get [RuntimeException] Unable to detect application namespace..
For exemple, I tried running php artisan make:controller ShoppingCartController and I get this error.
I looked online and a lot of people say it's a problem with the composer.json, but I tried composer diagnose and nothing stands out, and I updated composer and also tried composer dump-autoload.
And in my composer.json, I have this part I changed recently:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"projectName\\": "App/"
}
},
And I carefully looked to be sure there was no extra comma in the .json.
I'd like to still be able to use php artisan commands so I'd like to solve this.
Thanks,
When you rename your default namespace, the second parameter should be the name of the 'app' folder. so instead of
"projectName\\": "App/"
You should have
"projectName\\": "app/"
i want to ask, i get this message from git bash when i type this
$ composer require slim/slim "^3.0" then appear warn like this.
[RuntimeException]
Could not scan for classes inside "app/AppKernel.php" which does not appear
to be a file nor a folder
Can you help me guys ? sorry for bad english.
Thank you.
This answer can help https://stackoverflow.com/a/42934196/2110663
Check your composer.json file for occurencies app/AppKernel.php, and check your file structrue to be sure that you have this file in path.
In my case i had next part in composer.json (see classmap)
...
"autoload": {
"psr-4": {
"AppBundle\\": "src/AppBundle",
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
},
...
I tried to build dockerfile and cache at first vendors without code dependency. So
My dockerfile looks next
...
ADD composer.json .
ADD composer.lock .
RUN composer install
...
In this case filesystem does not contain app/AppKernel.php file, but composer.json file required for it.
To resolve this issue we have 2 options:
Update composer.json and remove from it mentions of problematic files
Add files, or check its existence in file-system
My solution was to add absent files before running composer install. Updated Dockerfile looks next:
ADD composer.json .
ADD composer.lock .
# Fix composer install issue with adding 2 lines below
ADD app/AppKernel.php app/
ADD app/AppCache.php app/
RUN composer install
Hope this hint might helps for somebody else
I created folder name - Birds and in this folder i created class Birds.php then add this to composer.json,
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Birds\\": "Birds/",
}
},
To load i have to run - composer dump-autoload -o and that's fine for first time to take that folder, But then i created new interface class like this :
namespace Birds\Validator;
interface BadgeInterface
{
public function test();
}
Interface class not working until in run composer dump-autoload,
My question is why i need to run this every time ? i am using laravel as framework.
Thanks
That's just how composer works. Laravel is just a PHP framework, so it is not able to run composer or other tool all the time for you.
If you need to automatically add files all the time, you can just use cron or Laravel's schedule command. It will do all the work for you every 5 minutes, for example.
This is cause when you create any class composer folder autoload.php can't update . that cause you need to run it manually . it clear you when you install any package at last command there run is composer autoload to update all reference in autoload.php file