TQ
dev.com

Blog about software development

Subscribe

Automatic REST API for SlimPHP 4

16 Aug 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 4 framework (10k stars). This is possible as they both adhere to the PHP-FIG's (PHP Framework Interop Group) HTTP standard PSR-7.

Install SlimPHP 4

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
cd slim-crud-api
mv ../composer.phar .
php composer.phar start

Now verify that the installation works by visiting:

http://localhost:8080/users/1

You should see the following JSON response:

{
    "statusCode": 200,
    "data": {
        "id": 1,
        "username": "bill.gates",
        "firstName": "Bill",
        "lastName": "Gates"
    }
}

This means that 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 "app/routes.php" to look like this:

<?php

declare(strict_types=1);

use Slim\App;
use Slim\Psr7\Request;
use Slim\Psr7\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.