Symfony 2 Bundle 中主文件的用途是什么?

What is the use of Main File in Symfony 2 Bundle?

Symfony 2 Bundle 中的 Main File 有什么用?

以下是文件的默认路径:

项目->src->BundleName->BundleName.php

例如:

Symfony_Project/src/AppBundle/AppBundle.php

以上文件的内容总是空白:

<?php

namespace AppBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AppBundle extends Bundle
{
}
  1. 这个文件在symfony中有什么用?
  2. 为什么我们可以使用这个文件?
  3. 是否强制? /我们可以删除它吗?
  4. 为什么是空的?

此文件可用于覆盖任何其他捆绑包(您的应用程序捆绑包/第三方捆绑包)及其资源。您可以为给定的包设置父包。例如,您正在包含 FosUserBundle 并且您想要覆盖它的某些 actions/layout 文件等。要完成此操作,请创建您的捆绑包 UserBundle.php。将 FosUserBundle 添加为其父级,如下所示:

// src/UserBundle/UserBundle.php
namespace UserBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class UserBundle extends Bundle
{
    public function getParent()
    {
        return 'FOSUserBundle';
    }
}

覆盖控制器:

// src/UserBundle/Controller/RegistrationController.php
namespace UserBundle\Controller;

use FOS\UserBundle\Controller\RegistrationController as BaseController;

class RegistrationController extends BaseController
{
    public function registerAction()
    {
        $response = parent::registerAction();

        // ... do custom stuff
        return $response;
    }
}

然后在您的 UserBundle 目录结构中,您可以覆盖 controllers/layout 文件等。

有关更多信息,请参阅此 link:https://symfony.com/doc/2.8/bundles/inheritance.html