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.
Architecture fit
The becklyn/javascript-context package provides a lightweight, Twig-centric solution for passing structured data from PHP to JavaScript, aligning well with Laravel’s templating layer (Blade/Twig via packages like symfony/twig-bridge). Its domain-based provider system enables modular data injection, which fits Laravel’s service container and dependency injection patterns. However, Laravel’s Blade templating (default) may require minor adaptation (e.g., custom Blade directives or Twig integration via laravel/tinker or spatie/laravel-twig). The package’s Symfony 5 compatibility is irrelevant for Laravel unless leveraging Symfony components directly (e.g., HTTP clients, process utilities), but its provider pattern could integrate neatly with Laravel’s service providers or facades.
Integration feasibility
spatie/laravel-twig) for Blade projects.@javascript_context()) via a custom package wrapper.HttpClient, Messenger), this package could reduce duplication by centralizing JS context logic.Technical risk
laravel-javascript-context) to adapt the output format.ContextProviderInterface), this could conflict with Laravel’s Symfony 6+ dependencies.<) may cause issues with modern JS frameworks (e.g., React/Vue) expecting raw JSON. Teams must validate output compatibility.Key questions
javascript_context() function be adapted for Blade? Will a custom Blade directive suffice, or is a full rewrite needed?textContent parsing) compared to native Laravel solutions like Json::encode() + @push('scripts')?laravel-mix/vite shared state) preferable?<, >, and & but not other XSS vectors (e.g., javascript:, data:). Does this meet the project’s security standards?response()->json() + Alpine.js/Vue reactivity) that avoid templating dependencies?Stack fit
spatie/laravel-twig and adapt templates (high effort).HttpClient, Messenger, or EventDispatcher. The provider pattern could centralize context across PHP services.Migration path
Json::encode() in Blade, Alpine.js stores).laravel-mix manifest, axios interceptors).javascript_context() with a Blade directive (e.g., @jsContext).<script> tag (or window.__JS_CONTEXT__ global).spatie/laravel-twig and test the package’s Twig integration.JSON.parse() compatibility, XSS safety).window.appData).Compatibility
set(), ContextProviderInterface) is stable, but Twig dependency breaks Laravel’s Blade-first workflow.composer why-not to detect risks.Sequencing
app, admin).laravel-mix/vite for build-time data injection).Json::encode() calls with the package’s JavaScriptContext for consistency.ContextProviderInterface to app services).Maintenance
symfony/http-client:^5.0 vs. Laravel’s ^6.0).Support
window.__INITIAL_STATE__) work.Scaling
response()->json()).Failure modes
javascript: checks) could expose apps to injection.Ramp-up
laravel-mix for build-time data, axios interceptors for runtime).window.__JS_DATA__ global or GraphQL subscriptions).How can I help you explore Laravel packages today?