1

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.

1
  • Found the solution (can't answer the question as it was closed): The tutorial is missing important config-entries (add services / _defaults / autowire and App\ / resource, etc. as in a regular Symfony-project) I can't post here as they are to long for a comment :( Commented Feb 21, 2020 at 10:46

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

-1

App\Controller\MicroController" has no container set, did you forget to define it as a service subscriber?"

It say you, the MicroController need be defined like a Service. According to the documentation, MicroController should not extend 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

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

2 Comments

Thanks for your answer. If MicroController should not extend AbstractController, than the documentation for the Micro Application is wrong, see symfony.com/doc/current/configuration/micro_kernel_trait.html - here MicroController extends AbstractController, but to make it work you have to apply my fix in framework.yaml. Either way is a workaround, I think the official page should be fixed in the first place :)
This is not a correct answer. If you don't extend anything with MicroController, you can't call $this->render, because it isn't defined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.