symfony6 - 在第三方包中定义和使用 services.yaml

问题描述 投票:0回答:0

我正在使用 symfony6.1 制作简单的“MyCoreBundle”(MystertyCoreBundle)如何制作捆绑包的文档.

我定义了我的包类 vendor/mysterty/core-bundle/CoreBundle.class

<?php

namespace Mysterty\CoreBundle;

use Symfony\Component\HttpKernel\Bundle\AbstractBundle;

class MystertyCoreBundle extends AbstractBundle
{
}

我在 vendor/mysterty/core-bundle/config/services.yaml 中定义了一些参数和配置作为默认值:

services:
  Mysterty\CoreBundle\Controller\CoreController:
    public: true
    calls:
      - method: setContainer
        arguments: ["@service_container"]

parameters:
  app.admin_email: "mymailATserver.com"

然后我在 vendor/mysterty/core-bundle/src/Controller/CoreController.php 中制作了简单的控制器:

<?php

namespace Mysterty\CoreBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;

class CoreController extends AbstractController
{

    #[Route('/', name: 'mty_default')]
    public function indexNoLocale(): Response
    {
        $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
        $supportedLangs = explode('|', $this->getParameter('app.supported_locales'));
        $lang = in_array($lang, $supportedLangs) ? $lang : $supportedLangs[0];
        return $this->redirectToRoute('mty_home', ['_locale' => $lang]);
    }

最后,我将包的路由添加到

symfony bundle symfony6
© www.soinside.com 2019 - 2024. All rights reserved.