1

Symfony version 5.4 and PHP version 7.4.*

In a normal Symfony project I can create a controller with routes (annotations) and It works fine. Now I have created a bundle which I puted inside a test project via composer. In this bundle I have created a controller with methods and routes (annotations).

When I use debug:router the routes of the bundle are not shown. I am not sure how to configurate this bundle to be able to use the controller routes on any project which It is puted in.

I have access to the Helper classes, but I don't know how to use the controller with a route.

Example inside the bundle: Inside my bundle I have a controller which renders a basic twig template:

<?php
    namespace MyWork\MyBundle\Controller;
    
    use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\Routing\Annotation\Route;
    
    class BundlePageController extends AbstractController
    {
    /**
     * @Route("/bundle_index", name="bundle_index")
     *
     * @return Response
     */
    public function index(): Response
    {
        return $this->render('bundle_page.html.twig');
    }
}

When I The solutions I found say, that I have to bind this in the router conf. But It did not work. I had created a routes.yml in my bundle and the idea was to import this routes.yml in the routes.yml from the project where the bundle will be used. But I don't know the parameters/naming, basicaly I do not know what to put there.

I have also tried to work with the symfony documentation but I am still struggling.

Do I have to load this controller as a service in the service.yml? I am kinda lost, any help is appreciated.

EDIT: I found here a post Symfony 5 Reusable Bundles controller has no container set, did you forget to define it as a service subscriber

Here, the controller was added in the service conf file and route conf file. I will try to use this.

EDIT: Now I have added the following

bundle service xml

<service 
    id="mywork_mybundle.controller.bundle_page_controller"
    class="MyWork\MyBundle\Controller\BundlePageController" 
    public="true"/>
<service 
    id="MyWork\MyBundle\Controller\BundlePageController" 
    alias="mywork_mybundle.controller.bundle_page_controller"/>

bundle routes xml

<route 
    id="mywork_mybundle_bundle_page_controller"
    path="/bundle-page" 
    controller="mywork_mybundle.controller.bundle_page_controller"/>

Now I have to figure out how to make this work in the project.

EDIT: Route is now showing in debug:router but with error

I have changed the routes.xml to a yaml because I had problems importing this in the project yaml file. So now in the bundle I have this routes.yaml file:

show_list:
    path: /bundle/page/list
    controller: MyWork\MyBundle\Controller\BundlePageController::list

However when I try to access this route I get the following error The controller for URI "/bundle/page/list" is not callable: Controller "MyWork\MyBundle\Controller\BundlePageController" cannot be fetched from the container because it is private. Did you forget to tag the service with "controller.service_arguments"?

Does this mean It can not read the service.xml anymore because I changed my routes.xml to yaml? Like stated before the service contains the public key with true. So It should be public. I don't know what is causing the problem here.

Edit: Now in the bundle service xml I have created this line:

 <service id="Symfony\Bundle\FrameworkBundle\Controller\AbstractController">
     <tag name="controller.abstract_controller"/>
 </service>

And give It as a arguement to my controller

<service
    id="mywork_mybundle.controller.bundle_page_controller"
    class="MyWork\MyBundle\Controller\BundlePageController"
    public="true">
  <argument type="service" id="controller.abstract_controller"/>
</service>
<service
    id="MyWork\MyBundle\Controller\BundlePageController"
    alias="mywork_mybundle.controller.bundle_page_controller"/>

My idea is, maybe I have to load the abstract controller service first and give it to my controller. But I am honest, I have no idea...

Now I am getting "...mycontroller..." has no container set, did you forget to define it as a service subscriber?

bschlueter
  • 3,817
  • 1
  • 30
  • 48
Muster Max
  • 11
  • 3
  • I find it useful to look at Symfony's Web Profiler Bundle to setup routing and controllers. And yes you usually do want to define your controllers as services but on the other hand it's generally recommend to avoid autowire in bundles so it can be a bit tricky. – Cerad Aug 04 '22 at 19:47
  • Yikes. Looks like your question grew. The answer to your yaml/xml question is no, it does not matter if you user routes.yaml and services.xml. However you might consider shifting to yaml for your bundle services as well. At least until you get things working. Your new route should use the controller's service id (mywork. etc) instead of the controller classname and get rid of that controller alias. – Cerad Aug 05 '22 at 13:33
  • Does your page controller extend AbstractController because that takes a few extra bits as well. – Cerad Aug 05 '22 at 13:34
  • @Cerad Yes, the controller extends the AbstractController It's pretty much the same as shown in the example. – Muster Max Aug 05 '22 at 16:00
  • See the end of this: https://github.com/symfony/symfony/discussions/44628 In general you should not be using AbstractController in a bundle but it really does no harm. Once you get the service locator (aka container) injected then you will at least be one step further. – Cerad Aug 05 '22 at 16:56

1 Answers1

0

The project's config/routes.xml should import the bundle's routing file. Here are the examples given in "How to Include External Routing Resources":

    <!-- config/routes.xml -->
    <?xml version="1.0" encoding="UTF-8" ?>
    <routes xmlns="http://symfony.com/schema/routing"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/routing
            http://symfony.com/schema/routing/routing-1.0.xsd">
    
        <!-- loads routes from the given routing file stored in some bundle -->
        <import resource="@AcmeOtherBundle/Resources/config/routing.yml" />
    
        <!-- loads routes from the PHP annotations of the controllers
                 found in that directory -->
        <import resource="../src/Controller/" type="annotation" />
    
        <!-- loads routes from the YAML or XML files found in that directory -->
        <import resource="../legacy/routing/" type="directory" />
    
        <!-- loads routes from the YAML or XML files
                 found in some bundle directory -->
        <import resource="@AppBundle/Resources/config/routing/public/" 
             type="directory" />
    </routes>
bschlueter
  • 3,817
  • 1
  • 30
  • 48
Arleigh Hix
  • 9,990
  • 1
  • 14
  • 31
  • Thank you, I used now the yaml format becuase xml didn't worked for me. However I have now the problem stated in the post (new edit) please check It out. – Muster Max Aug 05 '22 at 09:22