1

I created a default symfony 6.2 application using the according composer command.

After that I created a "bundle-dev" directory to create a reusable bundle there (bundle-dev/test/plugin-bundle). I created the bundle exactly like it's described here https://symfony.com/doc/current/bundles/best_practices.html.

My file Structure:

plugin-bundle
├── config
├──── routes
├────── attributes.yaml
├──── services.yaml
├── src
├──── Controller
├────── DefaultController.php
├──── PluginBundle.php
├── composer.json (name: "test/plugin")

For testing I created a defaultController with just a defaultAction. Routing is very basic - as described here (see /blog): https://symfony.com/doc/current/routing.html#creating-routes-as-attributes

I also added a repository entry to my composer.json to create an entry in the vendor folder of the "base" symfony installation. (vendor/test/plugin)

So far everything works fine - I "activated" the bundle in the config\bundles.php and yeah nothing really happens. The test route I created isn't loaded ... I checked the router via bin/console debug:router and the route isn't listed there ...

I tried to solve the problem via:

  • clearing the cache
  • removing the vendor folder (and reinstall it)
  • recreating the autoload files composer dump-autoload

It seems like the bundle isn't really loaded ... but the debugger says it is - at least symfony lists the bundle in the configurations tab (of the debugger)

Do you have any ideas how to fix this?

1
  • Did you update your application's config/route.yaml to include the bundle's routes? If not you can look in the config/routes directory for some examples of how to access a bundle's routes. Commented Jan 18, 2023 at 17:24

1 Answer 1

2

I'm pretty sure that you have not told your application to load your bundle's routes. There is a natural tendency to think that the bundle itself can automatically provide routes but no, you always need to do something at the application level. I remember reading somewhere that is this because of security. You don't want bundles adding routes without the application knowing it but I never really followed that reasoning.

The example you posted with attributes.yaml is also a bit misleading. It is more of a transitional example for the application. The config shown in the docs is actually stored in the application's config/routes.yaml file. Just one more source of confusion.

In any event you just need to add a bundle specific file to your application's config/routes directory:

# my_project/config/routes/plugin.yaml
controllers:
    resource:
        path: '@PluginBundle/src/Controller/'
        namespace: Test\PluginBundle\Controller
    type: attribute
    prefix: /plugin # optional but not uncommon

And you should be good to go.

Here is a working example of defining bundle routes. There are quite a few pieces that have to be wired up properly.

Somewhat unrelated but you should probably follow the bundle naming conventions. The vendor becomes part of the bundle's name. Somethig like CorexPluginBundle with a namespace of Corex\PluginBundle. Doing so might save you from some refactoring down the line.

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

7 Comments

thanks for your very detailed answer. According to your hint with the loading of the routes file: I thought that this was the responsibility of the Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait of each symfony base installation. The kernel registers the bundles via registerBundles() and also loads the config files in configureRoutes() or configureContainer
i changed my routes config according to your answer - that also doesn't work :S
and last but no least one more question: It cant be correct that bundles cant add routes to a application itself - nearly every bundle needs at least a controller with a route - or am I wrong?
I tested my solution so I know it works but you do need to have various parts wired up correctly. Consider updating your question with your new application routes config file and your route attribute from your DefaultController.
Here is a repo with a working example. I suppose worse case is you could check your code in someplace and I'll take a look. If it is proprietary then you could create a sample bundle with one controller. I find it handy to have a reference implementation to look at later.
|

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.