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.
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.