Search code examples
phpsymfonysymfony5

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.

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:

enter image description here

This is the index.php to get the application started:

// 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:

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 (e.g. "/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?"

my composer.json looks like this:

"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.


Solution

  • 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.