Laravel Artisan error: Target class does not exist - php

Once our Laravel app is deployed to hosting (AWS Elastic Beanstalk) it's impossible to run php artisan from the ssh command prompt. Every time results in the following error:
In Container.php line 879:
Target class [request] does not exist.
In Container.php line 877:
Class "request" does not exist
Even just running php artisan without any parameters results in the same error. Locally everything is fine.
Strangely AWS runs certain bash php artisan commands as part of postdeploy hooks which work fine during deployment (eg. artisan config:cache). They work fine, but even if I disable that bash script and try to run those commands manually via ssh after deploy, I get the same error. Very confusing.
It's as if there's something happening after deployment that's breaking php artisan.
We're running PHP 8.0.18 and Laravel 9.0.1. I've tried composer dump-autoload, but I still get the same error while that's running.
Where would be the right place to start looking for the cause of this error message?

Related

Can't run Laravel Envoy with exec (works in command line but doesn't with Artisan::call)

I have an envoy script which I am triggering within Laravel Command.
php artisan proxy:update
public function handle() {
exec("envoy run update");
}
If I run this on command line as php artisan proxy:update, it works.
However if I run this inside my Laravel app as Artisan::call('proxy:configure'); it doesn't work.
In console whoami = vagrant; likewise in my command exec('whoami') is also vagrant.
If I change it to
$out = shell_exec('envoy run update');
dd($out);
In command-line it shows the output, but with Artisan::call(), it returns empty string.
What might be the issue for being able to use exec() with artisan command?
As far as I understand, php-fpm was blocking it. I tried using Symfony's Process instead of exec() and I was getting "terminated". Then I moved Artisan::call() to Artisan::queue() and it worked. I'd be happy to know if anyone has any other explanations.
Update:
I created a queue that is run by deployer user which has sudo privileges (normally www-data run my queues). Now it works perfectly.

Laravel Artisan command for soap call "Class 'SoapClient' not found"

I have an artisan command where I do a soap call.
So I use the SoapClient
use SoapClient;
When I test run my command from an url like this:
Route::get('test-command', function() {
Artisan::call('updateRegisterLogs');
});
Everything works great!
Now when I try to run it with artisan on my linux server to test the command to create a cronjob I get the following error:
[Symfony\Component\Debug\Exception\FatalThrowableError]
Class 'SoapClient' not found
The weird thing is I even get this error when I try a simple php artisan to see a list of all my commands.
Why does my artisan break from this? My code works fine but artisan doesn't.
The PHP version and also the CLI ini file could be different from the web one and when running command with artisan you are using the php cli.
Check with
php -r "echo phpinfo();"
If your php cli has soap enabled.

laravel artisan use of undefined constant STDIN - assumed 'STDIN' infinite loop

I am using laravel 5.6 and have the problem, when I use the command "php artisan vendor:publish" in the console, I get the following error:
[ERROR] Use of undefined constant STDIN - assumed 'STDIN'
Which provider or tag's files would you like to publish?
[0] Publish files from all providers and tags listed below
[1] Provider: Intervention\Image\ImageServiceProviderLaravel5
The problem is, that these messages appear infinite, until I close the console or after a long time it kills the process.
I looked for this issue on google, but only found the problem with stdin and the difference is, that the people who had that problem, didn't call artisan in the command line interface, but directly in a php script.
The same problem appears when I use "php artisan migrate"
I was getting the issue mentioned above while running artisan command to seed the db tables: Artisan::call('db:seed', [...]) while in production.
Adding the --force flag fixed my issue
Artisan::call('db:seed', [
'--force' => true
])
Make sure you use --force flag if you are in production.
I have found a solution for the problem:
I had to add the following line to the artisan file (in the laravel directory).
define('STDIN',fopen("php://stdin","r"));
Now it works.
It's still strange, because normally artisan should work out-of-the-box, without the need to change anything.
Add
if(! defined('STDIN')) define('STDIN', fopen("php://stdin","r"));
to your index.php file. I tried adding this to artisan file but didn't work but placing it in index.php did the trick.
My PHP version is v. 7.4 on Ubuntu 18.04
This problem is caused by application environment.
Change application env to local or add --force parameter after artisan command.
One thing to check is to ensure you are running the correct version of PHP for the version of Laravel.
php -v will show the php version
I was surprised to see that for me, the CLI version of PHP (which is what artisan uses) was really old.
I didn't realize my shared host had many different versions of PHP installed.
I was able to run artisan commands by using the command corresponding to the PHP version I needed to use: php7.1 artisan migrate

Can't use shell_exec for laravel artisan command in live server

I am using shell_exec command to run my artisan commands in the background. But when I run shell_exec in production server.
The route code as follows
Route::get('/test/exec', function () {
echo shell_exec('php ../artisan migrate:status 2>&1; echo $?');
});
it throws me error as follows.
PHP Fatal error: Cannot redeclare class Illuminate\Support\Traits\Macroable in /var/www/production/bootstrap/cache/compiled.php on line 6109 255
But when I run the same command in my local I got the output.
Laravel versin - 5.1.46
php version - PHP 5.5.9
os version - ubuntu
14.04
These are same on both servers. Where things went wrong. Please, some one helps me with this.
For this error try to run the following commands:
php artisan clear-compiled
php artisan optimize
this should regenerate compiled.php file.
As for executing artisan commands from within your code there are better ways than using shell_exec - for example using Laravel build in support for programmatically executing commands
The error
% Cannot redeclare class %
also occurs when you inject a class that has the same name as the one you are actively editing/modifying.
In your case, please make sure that you have not injected a class with the name "Macroable" elsewhere. If you have, make sure that the namespace is unique.

run composer and laravel (artisan) commands without ssh access

I want to test some Laravel applications on my basic shared host.
Currently I just upload my complete application including the vendor files, however this takes quite long.
Since I do not have ssh access to my host I'd like to know whether there's an option to run composer / artisan commands without this.
I found this link: Use Composer without ssh access to server (Second Answer) which describes how to run composer using http://phpshell.sourceforge.net/
However, I can change folders in the console etc. But I cannot run php commands - I always get internal server error.
Check if your shared hosting provider has console feature in their CP which allows to run shell commands. Maybe you'll be able to run commands from there.
As alternative, you could right your own artisan runner and call artisan commands from the code:
Artisan::call('migrate');
To run composer command from PHP code, use shell_exec:
shell_exec('composer update');

Categories