swisnl/laravel-javascript-data-response

0.1.1 2018-05-22 07:36 UTC

README

PHP from Packagist Latest Version on Packagist Software License Build Status Scrutinizer Coverage Scrutinizer Code Quality Made by SWIS

JavaScript data response macro for Laravel

Install

$ composer require swisnl/laravel-javascript-data-response

Laravel Service Provider

If you are using Laravel < 5.5 or have disabled package auto discover, you must add the service provider to your config/app.php file:

'providers' => [
    ...,
    \Swis\Laravel\JavaScriptData\ServiceProvider::class,
],

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.

SWIS

SWIS is a web agency from Leiden, the Netherlands. We love working with open source software.