我正在使用MyCoreBundle制作简单的“MystertyCoreBundle”(MystertyCoreBundle)。
我定义了包类供应商/神秘/核心-包/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]);
}最后,我将包的路由添加到\config\routes.yaml中
mysterty_core:
resource: "../vendor/mysterty/core-bundle/src/Controller/CoreController.php"
type: annotation
prefix: /下面是我在http://127.0.0.1:8000/上遇到的错误:
"Mysterty\CoreBundle\Controller\CoreController“没有容器集,您忘记将它定义为服务订阅服务器了吗?
我尝试使用默认操作和组件为所有symfony项目创建一个共享包。
解决方案(thx to helpers)
在loadExtension中定义MyOwnBundle.php函数:
<?php
namespace MyOwn\MyOwnBundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
class MyOwnBundle extends AbstractBundle
{
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
{
// load an XML, PHP or Yaml file
$container->import('../config/services.yaml');
}
}发布于 2022-09-10 06:10:12
看起来Symfiony不能autoconfigure你的控制器。尝试将#[AsController]属性添加到控制器类,或者将autoconfigure: true添加到services.yaml中的控制器服务定义中。
https://stackoverflow.com/questions/73668352
复制相似问题