6

I'm currently building a new bundle and, when I'm trying to include a view from that bundle into a view from the main App (directly under /templates), i've got the error :

There are no registered paths for namespace "ContentEditable" in template.html.twig at line 27.

Here is the concerned part of template.html.twig (using @ and / syntax) :

{% include '@ContentEditable/content-edition.html.twig' %}

And here is a screenshot of my files structure & the return of "bin/console debug:twig"

EDIT1

And my /config/bundles.php contains the line :

App\Paul\ContentEditableBundle\ContentEditableBundle::class => ['all' => true]

/EDIT1

Do you know what I'm doing wrong ? Is there some cache to refresh ? Why "debug:twig" doesn't even show my bundle ?

If you need further informations about the code, please let me know !

Thanks by advance !

EDIT2

Solved by @Cerad

And I just changed the namespace and the hierarchy.

Made a "Paul" repo @ the same level as src, and made the root namespace "Paul".

For people having problem with this kind of refactoring, don't forget to add something like this to your composer.json :

"autoload": {
    "psr-4": {
        "App\\": "src/",
        "Paul\\": "Paul/"
    }
},

and to add something like this to your "services.yaml" :

Paul\:
        resource: "../Paul/*"
        exclude: "../Paul/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}"

/EDIT2

Paul ALBERT
  • 271
  • 1
  • 3
  • 8

1 Answers1

11

Rename your bundle's template directory to views (instead of templates):

src\Paul\ContentEditableBundle\Resources\views

I know it is a bit counter intuitive that the automatic twig namespace generator looks for the views directory and not templates. I know at one point there was a push to change the bundle directory layout to match the somewhat new app layout but I guess it is still a work in progress.

As an alternative, you could register your own custom twig namespace:

# config/packages/twig.yaml
twig:
    # ...
    paths:
        '%kernel.project_dir%/src/Paul/ContentEditableBundle/templates': ContentEditable        

This would perhaps future proof your directory layout.

And while off-topic I would caution against trying to mix your bundle's source code with your app source and definitely avoid using the App namespace. Lots of unexpected things might happen especially with autowire. Maybe something like:

src/
srcx/Paul/ContentEditableBundle/ContentEditableBundle.php

Cerad
  • 48,157
  • 8
  • 90
  • 92
  • Wow, thanks ! That did the trick. Yes that's a bit inconsistent, seems to be a remainder of an ancestral version :) Thanks for the namespace advice, I'm gonna try to use Paul as the root namespace for everything homemade ;) ( I followed this page https://symfony.com/doc/current/bundles.html process, maybe it's not up to date ? ) – Paul ALBERT Oct 17 '19 at 13:20
  • I was just looking at the bundle naming convention. The link you showed does indeed suggest App\Paul\ContentEditableBundle while the ["best practices"](https://symfony.com/doc/current/bundles/best_practices.html#classes) implies Paul\ContentEditableBundle. So there is an inconsistency in the current docs. I have never seen an actual shared bundle using the App namespace. And of course the only reason to make a bundle is to share it with multiple apps. – Cerad Oct 17 '19 at 13:26
  • Yup, I think you're right and the official docs is wrong ! Cheers mate ! – Paul ALBERT Oct 17 '19 at 13:30