我正在尝试使用 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”并运行没有错误:
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",
我错过了什么?任何提示表示赞赏。