Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer. You can expect quick tips, links to interesting tutorials, opinions and packages. Want to learn the cool stuff? Then sign up now!

Exporting json for a mobile app using Laravel

Every year the city of Antwerp (my hometown :-)) organises lots of activities, such as an Ice rink, a Christmas market, a Santa Run and of course fireworks in the month of december. To inform people of which activities are happening where and when my team and I created a mobile app commissioned by the city council. The app is called “Winter in A” and is available on both Android’s Play Store and iOS’ App Store.

The administrators of the app can enter content in a custom installation of Blender, our Laravel based application.  Blender will write of a bunch of json-files that are read by the mobile apps.  Here’s the file for all the events (english localization): https://api.winterapp.be/en/events.json.  We use our homegrown laravel-fractal package to easily transform database records to json. Here’s the export handler that’s in charge of the exporting of events:

namespace App\Services\Export\ExportHandlers;

use App\Repositories\EventRepository;
use App\Services\Export\ExportHandler;
use App\Services\Export\Transformers\EventTransformer;

class Events implements ExportHandler
{
    /**
     * @var \App\Repositories\EventRepository
     */
    protected $eventRepository;

    public function __construct(EventRepository $eventRepository)
    {
        $this->eventRepository = $eventRepository;
    }

    /**
     * Get the json for the given locale.
     *
     * @param string $locale
     *
     * @return mixed
     */
    public function getJsonForLocale($locale)
    {
        return fractal()
            ->collection($this->eventRepository->getAllOnline())
            ->transformWith(new EventTransformer($locale))
            ->toJson();
    }
}

The EventTransformer itself:

namespace App\Services\Export\Transformers;

use App\Models\Event;
use App\Services\Export\Format;
use App\Services\ValueObjects\Period;
use League\Fractal\TransformerAbstract;
use Spatie\MediaLibrary\Media;

class EventTransformer extends TransformerAbstract
{
    /**
     * @var
     */
    protected $locale;

    public function __construct($locale)
    {
        $this->locale = $locale;
    }

    public function transform(Event $event)
    {
        return [
            'id' => $event->id,
            'name' => $event->translate($this->locale)->name,
            'text' => htmlToMarkdown($event->translate($this->locale)->text),
            ...
            'images' => $event->getMedia('images')->map(function (Media $media) {
                return [
                   'thumb' => $media->getUrl('thumb'),
                   'full' => $media->getUrl('full')
                ];
            }),
        ];
    }
}

To handle peaks in usage of the app we use CloudFlare. In this article they explain what they do. Cloudflare has this awesome feature called “Always online”, to make sure the API is online even if our server is not.

The first events of Winter in Antwerp are starting tomorrow. The last ones are scheduled in the first days of 2016. I’ll share some more tidbits of the code then.

Freek Van der Herten is a partner and developer at Spatie, an Antwerp based company that specializes in creating web apps with Laravel. After hours he writes about modern PHP and Laravel on this blog. When not coding he’s probably rehearsing with his kraut rock band.