为什么在Laravel的artisan命令中需要composer.json文件?

huangapple go评论69阅读模式
英文:

Why composer.json is required for artisan command in Laravel?

问题

I am creating a laravel package for production and removing the composer.json and composer. lock once the dependencies are installed. But when I ran any artisan command I get the below error

在创建 Laravel 生产环境的包并在安装依赖项后移除 composer.json 和 composer.lock 时,运行任何 artisan 命令时出现以下错误:

In Application.php line 1399:
在 Application.php 的第 1399 行:
file_get_contents(/var/www/project_dir/composer.json): Failed to open stream: No such file or directory
file_get_contents(/var/www/project_dir/composer.json):打开流失败:没有这样的文件或目录

I am excluding the composer.json from the production environment as the autoloading is done through vendor/autoload.php.

我在生产环境中排除了 composer.json,因为自动加载是通过 vendor/autoload.php 完成的。

I was doing the same thing previously as well but not sure the issue appeared suddenly when I am deploying the new release.

我之前也是在做同样的事情,但不确定问题是在部署新版本时突然出现的。

英文:

I am creating a laravel package for production and removing the composer.json and composer. lock once the dependencies are installed. But when I ran any artisan command I get the below error

In Application.php line 1399:
  file_get_contents(/var/www/project_dir/composer.json): Failed to open stream
  : No such file or directory

I am excluding the composer.json from the production environment as the autoloading is done through vendor/autoload.php.

I was doing the same thing previously as well but not sure the issue appeared suddenly when I am deploying the new release.

答案1

得分: 2

Laravel 的 Illuminate\Foundation\Application 类从你的 composer.json 文件中加载根应用程序命名空间。这并不是什么新鲜事,即使在 2015 年的 Laravel v6 版本中,我都能找到类似的代码。

如果你的代码在之前的 Laravel 版本中没有部署 composer.json 文件就能正常工作,但现在不行了,你应该检查一下你的应用程序发生了什么变化。

英文:

Laravel's class Illuminate\Foundation\Application loads the root application namespace from your composer.json. This is nothing new, I can find such code even in Laravel v6 from 2015.

If your code was working well in any prior version of Laravel without having a composer.json deployed, but is no longer, you should check what has changed in your application

答案2

得分: 1

经过调查,我发现问题是由于在App\Console\Kernel类的commands()函数中调用的以下代码引起的:

$this->load(__DIR__.'/Commands');

基本上,我们使用php artisan make:command注册了控制台命令,它创建了一个文件,放在commands目录下,这是没问题的。但是,vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php中的load方法尝试获取应用程序命名空间,由于缺少composer.json文件而失败。

为了处理这个问题,我从App\Console\Kernel类的commands()函数中删除了$this->load(__DIR__.'/Commands');这行代码,并在App\Console\Kernel类内部的$commands属性中手动注册了命令。

https://laravel.com/docs/8.x/artisan#registering-commands

英文:

After investigating the issue I have found that the issue is causing due to

$this->load(__DIR__.'/Commands'); code called in App\Console\Kernel class in commands() function.

Basically, we have registered the console commands using php artisan make:command, and it created the file inside the commands directory which is fine. But the load method from vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php try to get the application namespace that fails due to a missing composer.json file.

So, to handle this I have removed the $this->load(__DIR__.'/Commands'); line from commands() function inside the App\Console\Kernel class and registered the commands manually using $commands property within App\Console\Kernel class.

https://laravel.com/docs/8.x/artisan#registering-commands

huangapple
  • 本文由 发表于 2023年3月9日 13:15:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75680673.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定