代码之家  ›  专栏  ›  技术社区  ›  Select0r

带MicroKernelTrait的Symfony 5微应用程序抛出错误“控制器没有容器集”

  •  0
  • Select0r  · 技术社区  · 6 年前

    我试图用symfony5构建一个微型应用程序,单文件方法可以工作,但是使用Twig等的高级示例不行。

    我创建了一个测试项目,具体描述如下: https://symfony.com/doc/current/configuration/micro_kernel_trait.html ,我的目录结构和文件内容与示例中相同:

    enter image description here

    // 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,
            ]);
        }
    }
    

    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,
            ]);
        }
    }
    

    “在项目中调用我仍然是无效的(例如/10)。/ "

    我的作曲家.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",
    

    我错过了什么?如有任何提示,我们将不胜感激。

    0 回复  |  直到 6 年前
        1
  •  2
  •   Select0r    6 年前

    找到了:上面提到的教程在配置文件中缺少一些条目。

    # 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}'
    

        2
  •  -1
  •   jcarballo    6 年前

    App\Controller\MicroController“没有容器集,是否忘记将其定义为服务订户?”

    它说你,微控制器需要像服务一样定义。根据 documentation

    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,
            ]);
         }
    }
    
    推荐文章