From WP REST API to Craft Element API

A couple of weeks ago I rebuilt a car company website from Wordpress into craftcms. Wordpress was hacked and I didn't want to be busy in the future with updates and making the website as hack proof as possible.

That's why I decided to rebuild. Wordpress wasn't only used for the website but also for the occasion IOS and android app I build.

After rebuilding the website I realized that the app must be broken, because the WP api didn't exist anymore.

The app uses the Wordpress WP Api for displaying occasions in an app. Lucky me craftcms has the element api as a competitor.

I installed the plugin and tweaked the output. After that, the JSON looked exactly the same way as the WP api looks. Because of that I didn't have to build a new app version, so the current app version can continue :D

What i tweaked?

The element api wraps everthing in a "data" object the WP REST API doesn't . To change this you have to modify the "element api" plugin. Go to the next file:

elementapi > league > fractal > src > Serializer > ArraySerializer.php

modify line 30 to line 31

elementapi-wpapi.png#asset:47

To activate the elementapi plugin from craft you have make a file in the craft/config folder, the name off file should be "elementapi.php" in that file you can build your own endpoints.

Update:
Found a new way to remove the 'data' object wrapper:

Use the config setting 'first' => true and wrap the endpoint in a function

like this:

'api/link' => function() {
return [
'elementType' => 'Entry',
'first' => true,
'criteria' => ['section' => 'mysecton'],
'paginate' => false,
'transformer' => function(EntryModel $entry) {
return $something;
}
];
}



In the screenshot below you'll see mine occasions endpoint.

elementapi.png#asset:48

Conclusion.. if you decided to change from the WordPress camp to Craft, Craft has a solution for almost everything you can do with Wordpress. By the way, i also written a dutch article about the comparison between WordPress & Craft.