becklyn/javascript-context
Send server-side data from PHP to JavaScript via a Twig helper that renders a JSON script container. Set values through the JavaScriptContext service and optionally use context providers/domains to inject shared data automatically.
set("feature-flags", ["new-dashboard": true])).data-* attributes or global JS objects) when:
app vs. backend data separation).FeatureFlagProvider).Adopt if:
Look elsewhere if:
Blade or JSON::encode() may suffice).window.__INITIAL_STATE__ are lighter but less structured).For Executives: "This package lets us safely and cleanly share backend data (like user settings or feature flags) with JavaScript without exposing our API or bloating our frontend code. It’s like a ‘data bridge’ between PHP and JS—secure, maintainable, and scalable. For example, we could dynamically enable/disable features for users without redeploying the frontend. It’s a small investment now that pays off in faster iterations and reduced technical debt as we add more JS-driven features."
For Engineers: *"Pros:
window.appConfig = {...} with typed, escaped data via JavaScriptContext.app vs. admin contexts) without template clutter.<, >, and & to prevent XSS in injected data.Cons:
Recommendation: Adopt for new projects or greenfield features where data sharing between PHP/JS is needed. For existing projects, evaluate the effort to migrate from manual globals to this pattern. Low risk, high reward for maintainability."*
How can I help you explore Laravel packages today?