JavaScript data response macro for Laravel
$ composer require swisnl/laravel-javascript-data-response
This package adds a response macro (similar to Response::jsonp) which you can use just like any other response e.g.
Response::javascriptData('translations', ['en' => ['foo' => 'bar']]);
// or
response()->javascriptData('translations', ['en' => ['foo' => 'bar']]);
This will create the following response with the appropriate headers:
(function(){
window["translations"] = {
"en": {
"foo": "bar"
}
};
})();
The following is the default configuration provided by this package:
return [
/*
|--------------------------------------------------------------------------
| JavaScript data Namespace
|--------------------------------------------------------------------------
|
| The namespace to use for the JavaScript data using dot notation e.g. foo.bar will result in window["foo"]["bar"].
|
*/
'namespace' => '',
/*
|--------------------------------------------------------------------------
| Default json_encode options
|--------------------------------------------------------------------------
|
| The default options to use when json_encoding the data.
| These will be ignored if options are provided
| to the response macro/factory.
|
*/
'json_encode-options' => JSON_UNESCAPED_UNICODE,
/*
|--------------------------------------------------------------------------
| Pretty print
|--------------------------------------------------------------------------
|
| Should we add JSON_PRETTY_PRINT to the json_encode options.
|
*/
'pretty-print' => env('APP_ENV') !== 'production',
/*
|--------------------------------------------------------------------------
| Default response headers
|--------------------------------------------------------------------------
|
| The default headers for the JavaScript data response.
| These will be ignored if headers are provided
| to the response macro/factory.
|
*/
'headers' => [
'Content-Type' => 'application/javascript; charset=utf-8',
],
];
If you would like to make changes to the default configuration, publish and edit the configuration file:
php artisan vendor:publish --provider="Swis\Laravel\JavaScriptData\ServiceProvider" --tag="config"
Please see CHANGELOG for more information on what has changed recently.
$ composer test
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.
SWIS is a web agency from Leiden, the Netherlands. We love working with open source software.