The SitePoint PHP blog has a tutorial posted by author Younes Rafie covering the use of the Fractal library from The PHP League to create "pretty" JSON API output.

If you’ve built an API before, I’ll bet you’re used to dumping data directly as a response. It may not be harmful if done right, but there are practical alternatives that can help solve this small problem.

One of the available solutions is Fractal. It allows us to create a new transformation layer for our models before returning them as a response. It’s very flexible and easy to integrate into any application or framework.

They use a Laravel application as the base, creating a simple database of user information and relating users to roles. The tutorial then starts in on using Fractal, building out "transformers" for the data. These transformers take in the data/object and, inside of a "transform" method, modify the output and return a "prettier" version. They show it in use in a controller, outputting a collection of user data, only returning the name and email values. The tutorial also covers a few other Fractal-related topics including pagination, including sub-resources and eager loading.

On the Laravelista.com site there's a recent tutorial posted showing you how to create an API with Lumen and Fractal, a combination of the popular Laravel-based microframework and Fractal, an output handling library that makes it simpler to transform data to your needs. This is the second part of his series (part one is here), making some improvements on his previous examples.

In the previous post I used Larasponse to get Fractal working with Laravel, but recently I have switched to native Fractal package. One thing to notice is that in this post I will be showing you parts of code from a real project that I am working on called Treeline, instead of just example code like in the previous post. Be sure to check the Treeline repository if you're unclear on something.

He goes back through some of the setup process, getting Fractal installed via Composer and the creation of an ApiController to handle basic things like setting the status code and transforming the response to JSON. He shows how to add in the necessary routes for the "projects" path and creates the matching controller, collection and item classes that integrate the Fractal transformers to format their output as JSON. He also includes the code for the transformer classes themselves, showing you how to pull in and modify the data for the API's needs. He ends the post showing how to integrate other data (the "notes" into a "project" instance) and the resulting JSON output.

Phil Sturgeon as a new post to his site today talking about the importance of serialized API output and why it's important to think about what to share and how they're shared.

One section that seems to get a lot of feedback and questions is when I talk about serialization, which I refer to as “adding a presentation layer to your data”. [...] To PHP developers, they often consider serialization to be using the serialize() function. Yes, this is one form of serialization, but it’s not the only one. Another common serialization approach is of course to use the json_encode() function. [...] Excuse the drastically simplified chunk of code here, but the point is we’re taking a model (probably using an ORM) and returning the result directly. This seems fairly innocent, but leads to a range of problems.

He suggests that, when thinking about the data coming out of your API, you have to assume that every possible value could be shared if models are output directly. He gives the example of user passwords which, obviously, don't need to be shared at all. He includes an example of formatting the output with the Fractal library and why using something like that is important. He covers some of the topics to think about including attribute data types, renaming fields to make them more clear, the ability to pull from multiple data stores and the ability to version serializers. He ends the post with links to a few different serialization formats and some solutions (not just PHP ones) that can be used for the sort of handling he recommends.

The SitePoint PHP blog has a new tutorial posted by Alexander Cogneau that gives you an introductory walkthrough of Fractal, a PHP-based library that provides a translation and presentation layer for the output of your APIs.

If you ever developed for an API, you might have had troubles with changes of your database schema. If you didn’t happen to have a good implementation, you had to rework your whole model when changing some column names. In this article I will demonstrate how you can use Fractal as a layer between your models and JSON output. This post will show you how this package will make API development easier.

He walks you through getting the library installed (via Composer) along with Silex for the application handling and the Illuminate/database package from the Laravel framework. He also provides a SQL file to create the database for the application. The sample app handles music information, providing a simple song and artist list. It uses the Fractal package to transform the data using Transformer objects for each data type.