Random class not found errors in PHP - php

We get random errors in php (windows 7 with xampp):
PHP Fatal error: Class [NAME OF MY CLASS] not found
The class exists and the HTTP requests for the same call works 99% of the time, but sometimes we get this error. Why?
UPDATE:
I already run composer dump-autoload and the mapping is in the autoload_psr4.php file.
BUT, I found there's also a mapping that starts with the same path, but with an invalid folder, for example:
My class is: Path/SubPath/ClassX
In autoload_psr4.php i have (in this same order):
'Path\\SubPath\\' => array($vendorDir . '/company/project/src'),
'Path\\' => array($baseDir . '/src'),
The problem here is: in the second entry, the $baseDir . /src folder doesn't exists. Can this explain why I get random errors? (By sometimes getting the second mapping instead of the first one)

i think you need to run : composer dump-autoload.
composer dump-autoload 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
Then run the following three commands:
php artisan clear-compiled
composer dump-autoload
php artisan optimize
This will clear the current compiled files, update the classes it needs and then write them back out so you don't have to do it again.

This is a laravel specific issue. Try this commands:
composer dump-autoload -o
php artisan clear-compiled
php artisan optimize

Related

Laravel clear package auto-discovery cache of composer packages

I wanted to remove a package from laravel. I did composer remove vendor/package
It was all good on my dev, but when I deploy on production something went wrong and I cannot do anything now.
when I run
php artisan package:discover
I'm geting
In ProviderRepository.php line 208:
Class 'Laracasts\Flash\FlashServiceProvider' not found
I'm guessing I it is something to do with some kind of cache or maby config
but I cannot run this command,
php artisan config:clear
because I'm getting the same error message as above.
PS. I'm using Laravel 5.6
You may need clear the autoloader files if they get jammed up as it is looking at those cached files when php artisan config:clear runs, thus creating the jam.
Remove the following files and they will rebuild:
/bootstrap/cache/packages.php
/bootstrap/cache/services.php
I fixed my problem deleting a config file like so:
$rm bootstrap/cache/config.php
This file is basically a cache for config, if you want to have this file built for you just run a following artisan command:
$php artisan config:cache

laravel ReflectionException Class Country does not exist

I've read other issues about "ReflectionException Class ClassName does not exist" which refers to database seeders. I tried solutions from these questions and they didn't solve problem.
I've run into problem when tried to create model with command
php artisan make:model Country
(I've already had countries table)
after that I constantly get error
vagrant#homestead:~/Code/l53$ php artisan
[ReflectionException]
Class Country does not exist
What I did after that:
created Country model manually
http://joxi.ru/YmEyoL7CVNL0m6
added path to classmap
http://joxi.ru/J2b4QvLc1dMgr6
run:
composer update --no-scripts
composer dump-autoload -o
sudo composer self-update
deleted bootstrap/cache/services.php (there was only this file)
After every of these steps after running 'php artisan' I still receive:
[ReflectionException]
Class Country does not exist
and after running composer update I receive:
vagrant#homestead:~/Code/l53$ composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
Illuminate\Foundation\ComposerScripts::postUpdate
php artisan optimize
[ReflectionException]
Class Country does not exist
Script php artisan optimize handling the post-update-cmd event returned with error code 1
Any help is appreciated. Thanks in advance.
P.S. I use laravel 5.3 via homestead
I tried to remove vendor folder and 'composer update' (which recreated it) which didn't help.
but after 'vagrant reload' it (php artisan) worked... strange issue)

Why do I have to run "composer dump-autoload" command to make migrations work in laravel?

I have built some migration classes in my application to create the tables I need, but I keep getting errors. I need to run this command:
composer dump-autoload
Only then it works again as expected. Am I doing something wrong that generates this error or this is a normal behaviour with migrations?
Below is the error that I get when running the migration process:
[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'CreateVideoStatusTable' not found
OK so I think i know the issue you're having.
Basically, because Composer can't see the migration files you are creating, you are having to run the dump-autoload command which won't download anything new, but looks for all of the classes it needs to include again. It just regenerates the list of all classes that need to be included in the project (autoload_classmap.php), and this is why your migration is working after you run that command.
How to fix it (possibly)
You need to add some extra information to your composer.json file.
"autoload": {
"classmap": [
"PATH TO YOUR MIGRATIONS FOLDER"
],
}
You need to add the path to your migrations folder to the classmap array. Then run the following three commands...
php artisan clear-compiled
composer dump-autoload
php artisan optimize
This will clear the current compiled files, update the classes it needs and then write them back out so you don't have to do it again.
Ideally, you execute composer dump-autoload -o , for a faster load of your webpages. The only reason it is not default, is because it takes a bit longer to generate (but is only slightly noticable).
Hope you can manage to get this sorted, as its very annoying indeed :(
You should run:
composer dump-autoload
and if does not work you should re-install composer
Short answer: classmaps are static while PSR autoloading is dynamic.
If you don't want to use classmaps, use PSR autoloading instead.
My error was placing a function inside config/fortify.php, in my case it was an anonymous function that gave the error.
The definitive solution for this is to see what files I normally modify in the config/ folder.
In many places they say delete the files from bootstrap/cache/* but it didn't work in my case, and it didn't really make sense. In any case I leave you the commands:
php artisan clear-compiled
composer dump-autoload
php artisan optimize
But I really recommend that you go through the base laravel config files that you have configured. here you will find the error.

Laravel - Generating bootstrap/compiled.php without invoking the composer dump-autoload --optimize

The documented way to generate bootstrap/compiled.php is to run the artisan command:
artisan optimize
The problem with artisan optimize is that it runs composer dump-autoload --optimize which flattens out every PSR-0 loading class in composer_classmap.php file. If you are using a library like Zend with PSR-0 autoloading, this classmap file goes beyond 3000 lines. Which is very un-optimal to load on every web request, so I do not want to do that.
How do I simply generate the compiled.php file without populating the composer classmap file?
Looking at the code, it seems that there is no way to do that. But I changed the code to provide an option and made a pull request: https://github.com/laravel/framework/pull/3708.
EDIT
Pull request merged. Now you can run:
php artisan optimize --psr

Unable to update Laravel 4 with Composer (Update: bug?)

Today, I pulled down the laravel/laravel repository from Github. I then ran php composer.phar install (as I normally would on my system, with a command window in the project directory). However, when I ran php composer.phar update, I received this error:
Everything installed just fine, and Laravel works as it should.
Any ideas what could be causing this issue?
Edit 1
artisan exists in the root of the project, but it throws an exception when I attempt to run php artisan optimize:
Side Note 1
If I try the alternative method (quicker) of installing Laravel (php composer.phar create-project laravel/laravel), I get the following:
Edit 2
Upon installation, I also get the same error, where it claims it cannot find artisan. Therefore, the installation does not fully complete. I believe that it is stopping when it wants to compile classes (or something to that effect), and then write bootstrap/compiled.php. That file doesn't exist.
Here's the snap from the install:
Edit 3
It seems that Composer is looking for artisan in the drive root (C:\). Why is it doing this? Even if I specify -d on the update, it throws the error. (I picked this up from a hunch - simply copied artisan to the root of the drive and it found it - albeit, it obviously did not run...)
Solution Found:
Composer makes calls to php artisan <command> (as per the instruction in composer.json > scripts), but it does not see what directory it is running from (perhaps because it is an external command?).
So, I solved my initial problem by using an absolute path to artisan in composer.json.
Everything is working now. I just wish I knew how to get Composer to know that it is running from C:\LocalServer\lab\laravel, and not just C:\.
As i can see, your artisan file is missing. Can you post the exact steps on how you install it ?
Also, please follow http://laravel.com/docs/installation and http://niallobrien.me/2013/03/installing-and-updating-laravel-4/
I had this problem today too. My laravel folder inside the vendor was deleted after composer update. I ran composer install again and problem resolved.

Categories