Skip to content

Navigation Menu

Sign in
Appearance settings
Sign up
Appearance settings
Discussion options

I like to manage differents stimulus controllers between my backend & frontend (cause conflicts between libs on backend/frontend, ex: different bootstrap, ...) and possible different Importmap.

We can have multiple paths: https://symfony.com/bundles/StimulusBundle/current/index.html#configuration bug not controllers.json?

What is the best way to manage this?

You must be logged in to vote

Replies: 1 comment · 4 replies

Comment options

I would probably split this by entrypoints, not by controller_paths.

As I understand it, controller_paths lets you put controllers in several folders, but it still uses the same Stimulus setup. So it is good for organization, but not really for a clean frontend/admin split.

I would do something like this:

{# frontend layout #}
{{ importmap('frontend') }}

{# admin layout #}
{{ importmap('admin') }}

Then each entrypoint can import its own bootstrap/controllers:

// assets/frontend.js
import './frontend/bootstrap.js';

// assets/admin.js
import './admin/bootstrap.js';

If both sides can use the normal Symfony UX loader, I would keep it. If the dependencies really conflict, I would make two small bootstrap files and register only the controllers needed for each side.

So I would use controller_paths only to organize files, and separate importmap entrypoints for the actual frontend/admin split. I don't think multiple controllers.json files are the right path here.

You must be logged in to vote
4 replies
@ytilotti
Comment options

Hi @FmiKL,

I have already 2 different js for importmap :

  • assets/front.js
  • assets/admin.js

But if you want different things (configuration) on Symfony UX... I don't see.
If I follow your imports, 2 bootstrap to start startStimulusApp() but how to change config.

@FmiKL
Comment options

Yes, you're right, two entrypoints only solve the JS split.

I had a similar need for separating frontend/admin code, so my first thought was to split the entrypoints too.

If both front.js and admin.js call startStimulusApp(), I think they still use the same Symfony UX generated setup, so the same controllers.json / StimulusBundle config.

So maybe the split cannot be fully done with Symfony UX config only, at least not just with two entrypoints.

What I would try is to keep the normal Symfony UX setup for the controllers that can be shared, and for the part that really differs between front/admin, register those controllers manually in each bootstrap file.

So something like: Symfony UX for the common part, and manual Stimulus registration only for the conflicting frontend/admin part.

I don't know if there is a cleaner official way to have two different controllers.json configs per layout, but I didn't find one.

@ytilotti
Comment options

I think we can play with this: https://symfony.com/bundles/StimulusBundle/current/index.html#configuration

With Webpack it's possible, but with AssetMapper...

@FmiKL
Comment options

Yes, I agree.

With Webpack/Encore, you can point the bridge to a specific controllers.json, so it makes sense.

But with AssetMapper, I don't see an equivalent. It looks like @symfony/stimulus-bundle is generated from the global StimulusBundle config.

So maybe this is the missing part for AssetMapper: being able to have named loaders/configs, like front and admin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.