Redis, YAML & XML

In the last few days I've added two new serializers packages. This makes it really easy to add YAML and XML support to your API. Besides the two serializers a Redis Cache Provider package is also available. Redis is one of the fastest options available and it's rich features makes it a popular choice.

YAML & XML Serializers

The two serializers supports the usual Content-Type headers by default. The YAML serializer supports application/x-yaml, text/x-yaml and text/yaml while the XML Serializer supports application/xml.

Install the serializers by adding them as dependencies to your composer configuration:

$ php composer.phar require phapi/serializer-yaml:1.*
$ php composer.phar require phapi/serializer-xml:1.*

Last but not least: add the serializers to your configuration in the app/configuration/middleware.php file by adding them right after the JSON serializer:

<?php

/*
 * Serializer middleware serializers the response body to the format that the client prefers
 */
$pipeline->pipe(new \Phapi\Middleware\Serializer\Json\Json());

/*
 * The following serializers are NOT installed by default. See the documentation for
 * more information about how to install them before uncommenting the line(s) below.
 */
$pipeline->pipe(new \Phapi\Middleware\Serializer\Yaml\Yaml());
$pipeline->pipe(new \Phapi\Middleware\Serializer\Xml\Xml());

The deserializers should be added right after the JSON deserializer:

<?php

/*
 * Deserializer middleware that deserializes any content provided in the request.
 */
$pipeline->pipe(new \Phapi\Middleware\Deserializer\Json\Json());

/*
 * The following serializers are NOT installed by default. See the documentation for
 * more information about how to install them before uncommenting the line(s) below.
 */
$pipeline->pipe(new \Phapi\Middleware\Deserializer\Yaml\Yaml());
$pipeline->pipe(new \Phapi\Middleware\Deserializer\Xml\Xml());

Redis Cache Provider

The new Redis Cache Provider requires a working installation of Redis.

Install the provider by adding them as dependencies to your composer configuration:

$ php composer.phar require phapi/cache-redis:1.*

The configuration is easy to set up, edit the app/configuration/settings.php file and make sure these lines are uncommented:

<?php

/*
 * Redis example. Please note that the Redis Cache Provider is NOT included by default by
 * Phapi. Please see https://github.com/phapi/cache-redis for more information about how to
 * install the package. When the package is installed, uncomment and modify host and port
 * (if needed). Please note that this version of the Redis Cache Provider does NOT support
 * redis clusters.
 */
$container['cache'] = function ($container) {
    return new \Phapi\Cache\Redis\Redis($servers = [
        [
            'host' => 'localhost',
            'port' => 6379,
        ]
    ]);
};