为什么我部署的 Symfony 应用程序无法加载 bundles.php 文件?

Why can't my deployed Symfony application load the bundles.php file?

我有一个 Symfony 网络应用程序。我想将它部署到虚拟专用服务器。

我已经在 Laravel Homestead Vagrant box 中开发了应用程序,我正在通过 Github 操作进行部署。

在我的本地机器上,该应用程序在 Vagrant 环境中运行良好,但在将文件同步到实时服务器后,它给我一个错误屏幕。

Warning: require(/var/www/thebedechkacase.com/src/config/bundles.php): failed to open stream: No such file or directory

错误来自 Kernel.php 的第 20 行,看起来像这样

const CONFIG_EXTS = '.{php,xml,yaml,yml}';
public function registerBundles()
{
    $contents = require $this->getProjectDir().'/config/bundles.php';
    foreach ($contents as $class => $envs) {
        if ($envs[$this->environment] ?? $envs['all'] ?? false) {
            yield new $class();
        }
}

我不明白的是,根据错误信息和上面的代码,$this->getProjectDir()的输出解析为/var/www/thebedechkacase.com/src/thebedechkacase.com是我的应用程序所在的文件夹代码被存储),但 bundles.php 实际上位于 /var/www/thebedechkacase.com/config/.

为什么 Symfony 认为 projectDirsrc 中,为什么它不在根目录(又名 thebedechkacase.com/)中寻找 config?为什么这在我的开发环境中有效,但在实时服务器中无效?

其他信息: 我正在尝试使用我在 Github 操作中调用的 shell 脚本来设置必要的环境变量,但是当我在实时服务器 shell 上手动检查环境变量时,我发现没有设置任何环境变量,所以我的脚本可能一开始就坏了(这就是为什么你能够看到默认的 Symfony错误页面)。这可能与此有关还是这是另一个错误?

URL 哪里可以看到错误: https://thebedechkacase.com/

Github 项目回购: https://github.com/Cooty/the-bedechka-case

部署代码的动作: https://github.com/Cooty/the-bedechka-case/blob/develop/.github/workflows/main.yml

Symfony 版本:5.0

实时服务器是 运行 Ubuntu 20.04,Ngingx PHP-FPM 7.4 和 PHP 7.4.

@Cerad 引导我找到解决方案,我从 rsync 命令的文件列表中排除了 composer.json,我认为它只需要安装依赖项,而不是 Symfony 也从中读取在运行时。

Symfony 根据“composer.json”文件位置设置“项目目录”。 bootstrap 应用程序(网络和客户端)需要“项目目录”。

这就是为什么当您在没有“composer.json”的情况下部署您的应用程序时,例如。对于 Elasticbeanstalk,您需要像这样修改您的内核:

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;

class Kernel extends BaseKernel
{
    use MicroKernelTrait;

    /**
     * NOTICE:
     * We don't want to deploy composer.json that's being used to compute project dir, so we will specify it here manually.
     */
    public function getProjectDir()
    {
        return \dirname(__DIR__);
    }

注意重写的 getProjectDir() 方法。