PHP Vars To Js Transformer

Push PHP server-side variables into JavaScript with one call, via a Laravel facade or standalone

Library
Composer
v3.2.5
2,203stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
48/100Fair
Development Activity4
Maintenance20
Community68
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
58/100Fair
Architecture58
Code Quality55
Innovation50
Learning Curve70

PHP Vars To Js Transformer solves the recurring problem of getting server-rendered data into client-side JavaScript without hand-writing <script> blocks full of json_encode() calls. In Laravel it registers a JavaScript facade so a controller can call JavaScript::put(['user' => $user, 'age' => 29]) and have those variables automatically serialized and prepended as global JS variables to a chosen view (typically a footer partial), immediately usable as user/age in any script on the page.

Outside Laravel, the underlying Laracasts\Utilities\JavaScript\Transformer class works standalone: pass it an array of PHP variables and a namespace, and it returns a <script> block ready to inject anywhere in a template, making it usable in any PHP project, not just Laravel apps.

What You Get

  • A JavaScript facade for Laravel apps with a one-line JavaScript::put([...]) API to expose PHP data to JS
  • A framework-agnostic Transformer class usable in any PHP project outside Laravel
  • Configurable target view/namespace so variables can be bound to a specific partial (e.g. footer) or a custom JS namespace object
  • Automatic JSON-safe serialization of arrays, Eloquent models/collections, and scalars passed to put()
  • Laravel 5.5+ package auto-discovery, so no manual service-provider registration is required on modern Laravel versions

Common Use Cases

  • Passing an authenticated user object or app config from a Laravel controller into client-side JavaScript for a Blade view
  • Bootstrapping a frontend SPA/Vue/Alpine component with server-rendered initial state without a separate API call
  • Avoiding repetitive <script>var foo = {{ json_encode($foo) }};</script> boilerplate scattered across Blade templates
  • Sharing translation strings, feature flags, or CSRF tokens with JavaScript in a centralized, testable way

Under The Hood

Architecture - The package centers on a Transformer class that accumulates a key/value array via put()/putScript(), serializes it (safely handling Eloquent models and collections via their toArray()/toJson() methods), and renders it as a <script> block; a Laravel JavaScriptServiceProvider binds this transformer into the container, exposes it via a JavaScript facade, and hooks into the view-composer system to auto-prepend the generated script tag to the configured target view.

Tech Stack - Pure PHP with an optional thin Laravel integration layer (service provider, facade, config file published via vendor:publish); it has no JavaScript build tooling of its own since it only emits a static <script> tag for the browser to execute.

Code Quality - The repository carries a Travis CI badge and a test suite covering the transformer’s serialization and view-binding behavior; recent commit activity is low, consistent with a mature, feature-complete utility package rather than one under active expansion.

API Design - The public surface is intentionally minimal: JavaScript::put(array) for Laravel users and Transformer::put(array) standalone, both returning a ready-to-render script string, which keeps the learning curve close to zero for anyone already familiar with Laravel facades.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search