render bundle's controller component

Viewed 38

I'm trying to make a twig component for my Symfony6 own simple bundle but i don't find the correct "route" to call.

in _footer.html.twig :

<div class="col-xs-4 text-center socials">
    {{ render(controller('My\\Controller\\CoreController::socials')) }}
</div>

in vendor/my/core-bundle/src/Controller/CoreController.php

<?php

namespace My\CoreBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class CoreController extends AbstractController
{
    /**
     * Render socials component
     */
    public function socials(): Response
    {
        return $this->render('@My/Core/_socials.html.twig', ['socials' => $this->getParameter('my_core.socials')]);
    }
}

in config/packages/framework.yaml

framework:
    //...
    fragments: { path: /_fragment }

If i place the controller in the app, it works with 'App\\Controller\\CoreController::socials'. But if i put the controller in my bundle, i don't find the correct path and i have this log :

An exception has been thrown during the rendering of a template ("The controller for URI "/_fragment" is not callable: Controller "MyCore\Controller\CoreController" does neither exist as service nor as class.").

UPDATE - more info on bundle controller's configuration :

in vendor/my/core-bundle/composer.json:

    "autoload": {
        "psr-4": {
            "My\\CoreBundle\\": "src/"
        }
    },

in vendor/my/core-bundle/config/services.yaml:

  My\CoreBundle\Controller\CoreController:
    public: true
    autowire: true
    autoconfigure: true
    calls:
      - [setContainer, ['@Psr\Container\ContainerInterface']]
    tags:
      - "controller.service_arguments"
      - "container.service_subscriber"

Solution :

Twig uses the namespace defined in vendor/my/core-bundle/composer.json to find the controller, so here is the correct path to call in twig template :

<div class="col-xs-4 text-center socials">
    {{ render(controller('My\\CoreBundle\\Controller\\CoreController::socials')) }}
</div>
0 Answers
Related