[英]Symfony 5 micro application with MicroKernelTrait throws error "controller has no container set"
I'm trying to build a micro application with Symfony 5, the single-file approach works, but the advanced example using Twig, etc. does not.我正在尝试使用 Symfony 5 构建一个微型应用程序,单文件方法有效,但使用 Twig 等的高级示例无效。
I built a test-project following the exact description as published here: https://symfony.com/doc/current/configuration/micro_kernel_trait.html , I have the same directory structure and the same file contents as in the example:我按照此处发布的确切描述构建了一个测试项目: https://symfony.com/doc/current/configuration/micro_kernel_trait.html ,我具有与示例相同的目录结构和相同的文件内容:
This is the index.php to get the application started:这是用于启动应用程序的 index.php:
// public/index.php
use App\Kernel;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Symfony\Component\HttpFoundation\Request;
$loader = require __DIR__.'/../vendor/autoload.php';
// auto-load annotations
AnnotationRegistry::registerLoader([$loader, 'loadClass']);
$kernel = new Kernel('dev', true);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
And this is the MicroController with the (sample) action:这是具有(示例)操作的微控制器:
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
class MicroController extends AbstractController
{
/**
* @Route("/random/{limit}")
*/
public function randomNumber($limit)
{
$number = random_int(0, $limit);
return $this->render('micro/random.html.twig', [
'number' => $number,
]);
}
}
The method "configureContainer" in Kernel.php is called and runs without error: Kernel.php 中的“configureContainer”方法被调用并运行,没有错误:
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
{
$loader->load(__DIR__.'/../config/framework.yaml');
// configure WebProfilerBundle only if the bundle is enabled
if (isset($this->bundles['WebProfilerBundle'])) {
$c->loadFromExtension('web_profiler', [
'toolbar' => true,
'intercept_redirects' => false,
]);
}
}
but still the project does not run, calling a valid route (eg "/random/10" as in the example) gives me the error: " "App\\Controller\\MicroController" has no container set, did you forget to define it as a service subscriber? "但项目仍然没有运行,调用一个有效的路由(例如“/random/10”,如示例中所示)给了我错误:“ “App\\Controller\\MicroController”没有设置容器,您是否忘记将其定义为服务订阅者? ”
my composer.json looks like this:我的 composer.json 看起来像这样:
"doctrine/annotations": "^1.8",
"symfony/config": "^5.0",
"symfony/dependency-injection": "^5.0",
"symfony/framework-bundle": "^5.0",
"symfony/http-foundation": "^5.0",
"symfony/http-kernel": "^5.0",
"symfony/routing": "^5.0",
"symfony/twig-bundle": "^5.0",
"symfony/web-profiler-bundle": "^5.0",
"symfony/yaml": "^5.0",
What am I missing?我错过了什么? Any hint is appreciated.任何提示表示赞赏。
Found it: The mentioned tutorial is missing some entries in the configuration-file.发现它:提到的教程在配置文件中缺少一些条目。
# config/framework.yaml
framework:
secret: S0ME_SECRET
profiler: { only_exceptions: false }
is the original one, add this to the file to get the micro application working:是原始的,将其添加到文件中以使微应用程序正常工作:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
the above part is taken from the regular symfony-config-files and is somehow missing in the micro application-tutorial.以上部分取自常规的 symfony-config-files,在微应用程序教程中不知何故丢失。
App\\Controller\\MicroController" has no container set, did you forget to define it as a service subscriber?" App\\Controller\\MicroController”没有设置容器,是不是忘记定义为服务订阅者了?”
It say you, the MicroController need be defined like a Service.它说你,需要像服务一样定义微控制器。 According to the documentation , MicroController should not extend AbastracController.根据文档,微控制器不应扩展 AbasracController。
When using a controller defined as a service, you can still extend the AbstractController base controller and use its shortcuts.使用定义为服务的控制器时,您仍然可以扩展 AbstractController 基本控制器并使用其快捷方式。 But, you don't need to!但是,你不需要! You can choose to extend nothing, and use dependency injection to access different services.您可以选择不扩展任何内容,并使用依赖注入来访问不同的服务。 Read more 阅读更多
So your controller should be defined as follows:所以你的控制器应该定义如下:
namespace App\Controller;
use Symfony\Component\Routing\Annotation\Route;
class MicroController
{
/**
* @Route("/random/{limit}")
*/
public function randomNumber($limit)
{
$number = random_int(0, $limit);
return $this->render('micro/random.html.twig', [
'number' => $number,
]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.