phapi / serializer-jsonp
Phapi JSONP Serializer is a middleware based serializer that converts arrays to JSON and adds the provided (validated) callback method name.
Installs: 1 518
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=5.6.0
- phapi/contract: 1.*
- phapi/exception: 1.*
- phapi/serializer: 1.*
Requires (Dev)
- codeclimate/php-test-reporter: dev-master
- mockery/mockery: 0.9.*
- phpunit/phpunit: 4.*
This package is not auto-updated.
Last update: 2021-02-05 22:52:32 UTC
README
The JSONP Serializer package contains only one middleware, for serialization. The JSONP serializer works perfectly with the JSON deserializer.
The serializer reacts if the Accept
header matches one of the supported mime types.
By default the supported mime types are: application/javascript
and text/javascript
. It is possible to add more mime types by passing an array to the constructor.
Installation
This middleware is not included by default in the Phapi Framework but if you need to install it it's available to install via Packagist and Composer.
$ php composer.phar require phapi/serializer-jsonp:1.*
Configuration
The serializer has two configuration options:
- The name of the request header that contains the callback function name. The default header name is set to X-Callback:
- Additional mime/types that the serializer should support.
<?php use Phapi\Middleware\Serializer\Jsonp\Jsonp; $pipeline->pipe(new Jsonp($callbackHeader = 'X-Callback', ['text/html']));
Note that the array with additional mime types passed to the constructor will be merged with the default settings.
See the configuration documentation for more information about how to configure the integration with the Phapi Framework.
Callback header
Please note that if the defined callback header cant be found or isn't a valid function name the serializer will serialize the response as JSON without including the provided callback function name.
HTTP Status code
The HTTP status code is included in the body if an error occurs and the HTTP status is changed to 200 since many/all clients will have problems handling a response with a HTTP status that isn't 200.
You can read more about the problem with JSONP and HTTP status codes here.
Phapi
This middleware is a Phapi package used by the Phapi Framework. The middleware are also PSR-7 compliant and implements the Phapi Middleware Contract.
License
Serializer JSONP is licensed under the MIT License - see the license.md file for details
Contribute
Contribution, bug fixes etc are always welcome.