Fractal: a Practical Walkthrough

Posted by Unknown on Monday, August 4, 2014

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.


Fractal as a package


Fractal is a project written in PHP, and part of The League of Extraordinary Packages. These packages all comply to several requirements, like usage of PHP-FIG and Unit Test coverage. Fractal is mainly developed by Phil Sturgeon and still receives regular improvements. It can also be used with Composer.


Setting up an environment


For demonstration purposes, I will now set up a framework using Silex and Illuminate/Database (the ORM component of Laravel). It doesn’t matter if you don’t have any experience with one of those. The things I will do are very straightforward and I will explain them as much as I can. If something is unclear do not hesitate to leave a comment.


I will now set up a framework. Note that if you don’t want to follow the steps, you can download all the code at the end of the article. Now start with creating a new folder inside root folder. We’ll start with creating our composer.json file, with all the dependencies that we need. In our case: Silex and Illuminate\Database. Create a composer.json file like this:



{
"require": {
"silex/silex": "~1.2",
"illuminate/database": "*"
},
}


Install the packages with composer install.


The database


I will take the example of an online music database. The database will provide information for several songs: the songname, the artist name, artist website, album name, release date and music label. In the beginning this will all be in one table. If you want to try it out yourself download the file 1.sql from this article’s repository and run it on your database.


Continue reading %Fractal: a Practical Walkthrough%




more

{ 0 comments... » Fractal: a Practical Walkthrough read them below or add one }

Post a Comment

Popular Posts