Home > Software design >  Symfony Reusable Bundle
Symfony Reusable Bundle

Time:01-19

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?

CodePudding user response:

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.

  • Related