Simple package which allows easy sharing of variables from PHP to JavaScript. This is a rewrite of the laracasts/utilities package. I was prompted to do this due to the aformentioned package requiring PHP 5.4+, and I disliked their design.
composer.json
"require": {
...
"rosio/php-to-javascript-variables": "~1.0"
}
config/app.php
'providers' => array(
...
'Rosio\PhpToJavaScriptVariables\PhpToJavaScriptVariablesServiceProvider',
),
controllers\HomeController.php
public function showWelcome()
{
JSLocalize::put(array(
'variableName' => 'variableValue',
'anotherVariable' => array(1, 2, 3)
));
return View::make('hello');
}
views\hello.php
<!doctype html>
<html lang="en">
<head>
...
{{ App::make('JSLocalizeDumper')->dump() }}
...
</head>
<body>
<script type="text/javascript">
alert(app.variableName);
</script>
</body>
</html>