Stimulus controllers in subfolders not working in Symfony

Viewed 1238

Good morning. I am experiencing problems with Stimulus controllers and namespaces following the name convention specified in Stimulus documentation.

When I use my controllers like this everything works as expected:

/controllers/gifts_controller.js
data-controller="gifts"

If I use the controller this way, nothing works:

/controllers/frontend/gifts_controller.js
data-controller="frontend--gifts"

Stimulus documentation specifies that the name convention for controllers in subfolders must be like the second example, but it is not working. No errors, it's just like if it doesn't exist.

Any ideas?

Edit: Sorry I wrote something wrong

1 Answers

The suffix _controller.js in file names is the convention used by Stimulus to retrieve the controllers in your directory structure. It should not be included in the data-controller attribute.

As stated in the documentation article you shared :

Name your controller files [identifier]_controller.js, where [identifier] corresponds
to each controller’s data-controller identifier in your HTML.

The double dash (--) replaces any forward slash (/) in a namespaced (subfolder) structure.

In your case, for the file controllers/frontend/gifts_controller.js the identifier is frontend--gifts. So, in the data attribute of your HTML element, you should refer to your controller as data-controller="frontend--gifts".

Related