swisnl / laravel-javascript-data-response
JavaScript data response macro for Laravel
Installs: 30 232
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 4
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- ext-json: *
- laravel/framework: ^10.0|^11.0
Requires (Dev)
- laravel/pint: ^1.15
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^10.0
README
JavaScript data response macro for Laravel
Install
$ composer require swisnl/laravel-javascript-data-response
Usage
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" } }; })();
Configuration
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', ], ];
Publish Configuration
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"
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email security@swis.nl instead of using the issue tracker.
Credits
License
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 ❤️ Open Source
SWIS is a web agency from Leiden, the Netherlands. We love working with open source software.