TQ
dev.com

Blog about software development

Subscribe

Automatic REST API for Laravel

22 Apr 2019 - by 'Maurits van der Schee'

This is the third post in a blog series on how to integrate PHP-CRUD-API (a single PHP file that adds a REST API to your database) into popular PHP web frameworks. In this post we integrate PHP-CRUD-API with Laravel, a PHP web framework. With 50k Github stars Laravel seems to be more popular than Symfony (20k stars) and SlimPHP (10k stars), the frameworks we have integrated with in earlier posts. Integration with these frameworks is possible as they all adhere to the PHP-FIG's (PHP Framework Interop Group) HTTP standard PSR-7.

Install Laravel

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

wget https://getcomposer.org/composer.phar
php composer.phar create-project laravel/laravel laravel-crud-api
cd laravel-crud-api/
mv ../composer.phar .
php artisan serve

Now verify that the installation works by visiting:

http://127.0.0.1:8000

If you see the Laravel logo page, 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 symfony/psr-http-message-bridge
php composer.phar require laminas/laminas-diactoros
php composer.phar require mevdschee/php-crud-api

Change the "routes/api.php" to the following content to define our new API route:

<?php

use Psr\Http\Message\ServerRequestInterface;
use Tqdev\PhpCrudApi\Api;
use Tqdev\PhpCrudApi\Config;

Route::any('/{any}', function (ServerRequestInterface $request) {
    $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;
})->where('any', '.*');

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://127.0.0.1:8000/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.

Query parameters with brackets

To have prettier URLs, PHP-CRUD-API supports multiple query parameters with the same name. Laravel does support this. Instead you need to use the (PHP official) query parameter "array" syntax that uses brackets "[]". So instead of:

/api/records/posts?filter=user_id,eq,1&filter=category_id,eq,2

you need to make it look a bit less nice with "filter" replaced by "filter[]":

/api/records/posts?filter[]=user_id,eq,1&filter[]=category_id,eq,2

Note that this also applies to all other query parameters, so "join" becomes "join[]".

Further reading

Laravel is often used to build APIs, because it is easy to use, standard compliant and very popular. 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 Laravel go to:

https://laravel.com/

Other frameworks

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

Enjoy!


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