具有 MicroKernelTrait 的 Symfony 5 微应用程序抛出错误 "controller has no container set"
Symfony 5 micro application with MicroKernelTrait throws error "controller has no container set"
我正在尝试使用 Symfony 5 构建一个微型应用程序,单文件方法有效,但使用 Twig 等的高级示例无效。
我按照此处发布的确切描述构建了一个测试项目:https://symfony.com/doc/current/configuration/micro_kernel_trait.html,我具有与示例中相同的目录结构和相同的文件内容:
这是启动应用程序的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);
这是带有(示例)操作的微控制器:
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,
]);
}
}
调用了Kernel.php中的方法"configureContainer"并且运行s没有错误:
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,
]);
}
}
但项目仍然没有 运行,调用有效路由(例如示例中的“/random/10”)给我错误:“"App\Controller\MicroController"没有设置容器,是不是忘记定义为服务订阅者了?"
我的 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",
我错过了什么?任何提示表示赞赏。
找到它:提到的教程在配置文件中缺少一些条目。
# config/framework.yaml
framework:
secret: S0ME_SECRET
profiler: { only_exceptions: false }
是原始的,将其添加到文件中以使微应用程序正常运行:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
以上部分取自常规的 symfony-config-files 并且在微型应用程序教程中不知何故缺失。
App\Controller\MicroController" has no container set, did you forget to define it as a service subscriber?"
你说的,微控制器需要像服务一样定义。根据 documentation,MicroController 不应扩展 AbastracController。
When using a controller defined as a service, you can still extend the AbstractController base controller and use its shortcuts. But, you don't need to! You can choose to extend nothing, and use dependency injection to access different services. Read more
所以你的控制器应该定义如下:
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,
]);
}
}
我正在尝试使用 Symfony 5 构建一个微型应用程序,单文件方法有效,但使用 Twig 等的高级示例无效。
我按照此处发布的确切描述构建了一个测试项目:https://symfony.com/doc/current/configuration/micro_kernel_trait.html,我具有与示例中相同的目录结构和相同的文件内容:
这是启动应用程序的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);
这是带有(示例)操作的微控制器:
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,
]);
}
}
调用了Kernel.php中的方法"configureContainer"并且运行s没有错误:
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,
]);
}
}
但项目仍然没有 运行,调用有效路由(例如示例中的“/random/10”)给我错误:“"App\Controller\MicroController"没有设置容器,是不是忘记定义为服务订阅者了?"
我的 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",
我错过了什么?任何提示表示赞赏。
找到它:提到的教程在配置文件中缺少一些条目。
# config/framework.yaml
framework:
secret: S0ME_SECRET
profiler: { only_exceptions: false }
是原始的,将其添加到文件中以使微应用程序正常运行:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
以上部分取自常规的 symfony-config-files 并且在微型应用程序教程中不知何故缺失。
App\Controller\MicroController" has no container set, did you forget to define it as a service subscriber?"
你说的,微控制器需要像服务一样定义。根据 documentation,MicroController 不应扩展 AbastracController。
When using a controller defined as a service, you can still extend the AbstractController base controller and use its shortcuts. But, you don't need to! You can choose to extend nothing, and use dependency injection to access different services. Read more
所以你的控制器应该定义如下:
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,
]);
}
}