fet/laravel-php-to-js

A simple Laravel package to pass PHP variables to JavaScript.

0.2.4 2025-04-03 22:56 UTC

This package is auto-updated.

Last update: 2025-04-03 22:57:26 UTC


README

The fet/laravel-php-to-js package provides a simple way to pass PHP variables to JavaScript in your Laravel applications.

Installation

  1. composer require fet/laravel-php-to-js
  2. php artisan vendor:publish --provider="Fet\PhpToJs\PhpToJsServiceProvider" --tag="config"

Configuration

// config/phptojs.php

return [
    'namespace' => 'phpToJs',
];

Usage

You can use the \Fet\PhpToJs\PhpToJsFacade facade to add variables that are then transformed to JavaScript.

use Fet\PhpToJs\PhpToJsFacade;

PhpToJsFacade::add(['foo' => 'bar']);

PhpToJsFacade::setNamespace('test');
PhpToJsFacade::add(['bat' => 'baz']);

The variables can be accessed in JavaScript as follows:

window.phpToJs.foo // Outputs: bar
window.test.bat // Outputs: baz

The default namespace is phpToJs.

Tests

Run the tests with:

composer test