TQ
dev.com

Blog about software development

Subscribe

Automatic REST API for SlimPHP 3

20 Apr 2019 - by 'Maurits van der Schee'

Today, about 4 years after the initial commit, the promise of "upload a single PHP file to add a REST API to your database" is still very much alive. It is now possible to use PHP-CRUD-API (2k stars) as a library executed on an endpoint in the SlimPHP 3 framework (10k stars). This is possible as they both adhere to the PHP-FIG's (PHP Framework Interop Group) HTTP standard PSR-7.

Updated! SlimPHP 4 has been released!

This tutorial is for SlimPHP 3, while SlimPHP 4 has been released. It is recommended that you follow the updated tutorial here:

https://tqdev.com/2019-automatic-api-slimphp-4

This post will be available for people that have installed SlimPHP 3 and want to run this (older) version.

Install SlimPHP 3

You need to run the following commands on your Linux system:

wget https://getcomposer.org/composer.phar
php composer.phar create-project slim/slim-skeleton slim-crud-api ^3
cd slim-crud-api
mv ../composer.phar .
php composer.phar start

Now verify that the installation works by visiting:

http://localhost:8080

If you see the Slim logo, then your configuration is okay.

Install PHP-CRUD-API

In order to add the automatic API library you need to run:

php composer.phar require mevdschee/php-crud-api

You now change the file "src/routes.php" to look like this:

<?php
use Slim\App;
use Slim\Http\Request;
use Slim\Http\Response;

// Note these extra use statements:
use Tqdev\PhpCrudApi\Api;
use Tqdev\PhpCrudApi\Config;

return function (App $app) {
    $container = $app->getContainer();

    // Add this handler for PHP-CRUD-API:
    $app->any('/api[/{params:.*}]', function (Request $request,
        Response $response, array $args) use ($container) {

        $config = new Config([
            'username' => 'php-crud-api',
            'password' => 'php-crud-api',
            'database' => 'php-crud-api',
            'basePath' => '/api',
        ]);
        $api = new Api($config);
        $response = $api->handle($request);
        return $response;
    });
};

Replace the string "php-crud-api" in the above code to match the username, password and database of your setup (preferably reading them from environment variables). You should see your application running at:

http://localhost:8080/api/records/posts

Replace "posts" with the name of any table in your database. If everything works as expected, then you should see the contents of the table in JSON format.

Further reading

SlimPHP is often used to build APIs, because it is easy to use, standard compliant and light-weight. PHP-CRUD-API on the other hand saves you a lot of time, as it offers you a full featured REST API for all tables in your database without any coding (it uses reflection). Now that you can combine the two you can have the best of both worlds. For more information about PHP-CRUD-API and what you can do with it read the Github README at:

https://github.com/mevdschee/php-crud-api

For more information on SlimPHP go to:

http://www.slimframework.com/

Other frameworks

Laravel and Symfony 4 (alternatives for SlimPHP) can be integrated in a similar way. Read more:

Enjoy!


PS: Liked this article? Please share it on Facebook, Twitter or LinkedIn.