Looking to hire Laravel developers? Try LaraJobs

laravel-php-to-js maintained by fet

Description
A simple Laravel package to pass PHP variables to JavaScript.
Last update
2025/04/04 00:56 (dev-main)
License
Downloads
642

Comments
comments powered by Disqus

Introduction

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