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?