laravel-php-to-js maintained by fet
Description
A simple Laravel package to pass PHP variables to JavaScript.
Author
Last update
2025/04/04 00:56
(dev-main)
License
Downloads
642
Tags
Introduction
The fet/laravel-php-to-js package provides a simple way to pass PHP variables to JavaScript in your Laravel applications.
Installation
composer require fet/laravel-php-to-jsphp 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