How to build a RESTful API in Slim 3 – Part 1: Application setup

Slim framework took off in 2013 when Josh Lockhart released the first version of the framework. The idea was to create a PHP micro framework that helps PHP developers quickly and easily write web applications and APIs. Over time, Slim has risen to risen to the point of competing with other PHP frameworks, like Laravel, Symphony, Codeigniter amongst others.

In this tutorial, you will learn how to build a voucher pool microservice application using Slim 3 framework. We will build a voucher pool which will be a collection of voucher codes that customers can use on a website in order to get discounts. When a customer uses a voucher code once, we want to know who used the code and when it was used. Part one of this article will focus on our application setup and how to create our database migrations.

What we want to achieve:

  • For a given special offer and an expiration date, we will generate for each recipient a unique voucher code.
  • We need to provide an endpoint, which will receive a voucher code and email and validates the voucher code. In case it is valid, return the percentage discount and set the date of usage.
  • For any given email, we need to return all valid voucher codes with the names of the user and the name of the special offer.

Prerequisites

  • Basic knowledge of PHP
  • Have Composer installed on your local machine
  • Have PHP setup on your local machine (version >= 7.1.3)
  • Git installed on your local machine

Setting up the application

To set up Slim 3, we will use a Laravel-like boilerplate. If you use Laravel a lot, you will like this boilerplate. Run this command in your project directory.

$ composer create-project codecourse/slender voucherPool

You will notice you have a new project in your project directory. Next, navigate into the directory and install third-party packages needed for our application to work. Since we have a Laravel-like structure, we can as well integrate Laravel's eloquent ORM and enjoy the goodness of manipulating our database using the object-oriented paradigm.

Run this command in your terminal to include illuminate database component in our codebase:

$ composer require illuminate/database

Once this is done, our application needs to know how to handle Eloquent queries. To achieve this, create a database file inside the bootstrap directory.

$ touch bootstrap/database.php

Open the database file and replace the content with this:

//  bootsrap/database.php
<?php

use Illuminate\Database\Capsule\Manager as Capsule;

$config  = $container['settings']['database'];
$capsule = new Capsule;
$capsule->addConnection(array_merge($config,[
    'charset'   =>  'utf8',
    'collation' =>  'utf8_unicode_ci'
]));

$capsule->bootEloquent();
$capsule->setAsGlobal();

Next, we need to include this file in our application boot process. Open the bootstrap/app.php file and edit as follows:

[...]
require_once __DIR__ . '/database.php';
require_once __DIR__ . '/../routes/web.php';
[...]

For this guide, we will use an SQLite database. Open the .env file and include the following database settings:

DB_DRIVER=sqlite
DB_DATABASE=_insert_absolute_path_to_sqlite_file

Remember to insert the absolute path to our SQLite file here: ./db/database.db

Once that is done, we also want to load our database credentials in our boot process. Open the bootstrap/app file one more time and replace the content of the $app variable with this:

// /bootstrap/app.php
[...]
$app = new Slim\App([
    'settings' => [
        'displayErrorDetails' => getenv('APP_DEBUG') === 'true',

        'app' => [
            'name' => getenv('APP_NAME')
        ],

        'views' => [
            'cache' => getenv('VIEW_CACHE_DISABLED') === 'true' ? false : __DIR__ . '/../storage/views'
        ],

        'database' => [
            'driver'    => getenv('DB_DRIVER'),
            'database'  => getenv('DB_DATABASE'),
        ]
    ],
]);
[...]

Next, we will use Phinx to manage our database migrations. Run this command to install Phinx in our codebase:

$ composer require robmorgan/phinx

Awesome!

Now that we have Phinx installed, we need to create the folder where Phinx will run our migration files from and finally create a database.db for our SQLite. Create this folder in your project directory

$ mkdir -p db/migrations
$ touch db/database.db 

This command will create a migration folder inside of a db folder.

With Phinx installed, we also need to initialize it in our codebase. Run this command to initialize Phinx:

$ vendor/bin/phinx init

You will notice a new file in your codebase, phinx.yml. The file stores your database credentials and points phinx to the location of your migrations and seeder files

Next, in other for Phinx to communicate with our database, open the phinx.yml file and edit as follows

    development:
        adapter: sqlite
        name: ./db/database
        suffix: ".db"    # Defaults to ".sqlite3"

Creating migration files

Before creating our migration files, we need to understand our database schema. We will have three tables, users, offers, vouchers. (See image below)

To create our migration files, run the following commands on your terminal

$ vendor/bin/phinx create CreateUsersTable
$ vendor/bin/phinx create CreateOffersTable
$ vendor/bin/phinx create CreateVouchersTable

This command will create our users migration file inside the db/migrations/ directory.

Next, open the users migration file and replace the content with this:

// /db/migrations/timestamp_create_users_table.php
<?php
use Phinx\Migration\AbstractMigration;

class CreateUsersTable extends AbstractMigration
{
    /**
     * Migrate Up.
     */
    public function up()
    {
        $users = $this->table('users');
        $users->addColumn('name', 'string', ['null' => true])
              ->addColumn('email', 'string')
              ->addColumn('created_at', 'datetime', ['default' => 'CURRENT_TIMESTAMP'])
              ->addColumn('updated_at', 'datetime', ['default' => 'CURRENT_TIMESTAMP'])
              ->addColumn('deleted_at', 'datetime', ['null' => true])
              ->save();
    }
    /**
     * Migrate Down.
     */
    public function down()
    {
        $this->dropTable('users');
    }
}

With that done, open the offers migration file and replace the content with this:

// /db/migrations/timestamp_create_offers_table.php

<?php
use Phinx\Migration\AbstractMigration;

class CreateOffersTable extends AbstractMigration
{
     /**
     * Migrate Up.
     */
    public function up()
    {
        $offers = $this->table('offers');
        $offers->addColumn('name', 'string')
              ->addColumn('discount', 'integer')
              ->addColumn('expires_at', 'datetime')
              ->addColumn('created_at', 'datetime', ['default' => 'CURRENT_TIMESTAMP'])
              ->addColumn('updated_at', 'datetime', ['default' => 'CURRENT_TIMESTAMP'])
              ->addColumn('deleted_at', 'datetime',['null' => true])
              ->save();
    }
    /**
     * Migrate Down.
     */
    public function down()
    {
        $this->dropTable('offers');
    }
}

Finally, open the vouchers migration file and replace the content with this:

// /db/migrations/timestamp_create_vouchers_table.php
<?php
use Phinx\Migration\AbstractMigration;

class CreateVouchersTable extends AbstractMigration
{
     /**
     * Migrate Up.
     */
    public function up()
    {
        $voucher = $this->table('vouchers');
        $voucher->addColumn('code', 'string')
              ->addColumn('offer_id', 'integer', ['null' => true])
              ->addColumn('user_id', 'integer', ['null' => true])
              ->addColumn('is_used', 'integer',['default' => 0])
              ->addColumn('date_used', 'datetime', ['null' => true])
              ->addColumn('created_at', 'datetime', ['default' => 'CURRENT_TIMESTAMP'])
              ->addColumn('updated_at', 'datetime', ['default' => 'CURRENT_TIMESTAMP'])
              ->addColumn('deleted_at', 'datetime', ['null' => true])
              ->addForeignKey('user_id', 'users', 'id', ['delete'=> 'SET_NULL', 'update'=> 'NO_ACTION'])
              ->addForeignKey('offer_id', 'offers', 'id', ['delete'=> 'SET_NULL', 'update'=> 'NO_ACTION'])
              ->addIndex(['code'], ['unique' => true]) 

              ->save();


    }
    /**
     * Migrate Down.
     */
    public function down()
    {
        $this->dropTable('voucher');
    }
}

Now that we have created our migration files, we need to run them. Run this command on your terminal:

$ vendor/bin/phinx migrate

The last package we will install is the validation package, we will need this when we want to validate all inputs supplied to our application. Open your terminal and run this command:

$ composer require awurth/slim-validation

Next, we also need to include our validation package in our application boot process. Open the bootstrap/app.php file and edit as follows:

[...]
$container['validator'] = function ($container) { return new Awurth\SlimValidation\Validator; };
require_once __DIR__ . '/database.php';
require_once __DIR__ . '/../routes/web.php';
[...]

Conclusion

In this chapter, we have looked at how to set up our Slim 3 framework application. We set up our application to use Laravel’s Eloquent to communicate with our database. We created our database schema and we set up Phinx to help with our migration files. Finally, we included a validation package to ensure that users submit the right data to our API.

In the next chapter, we will proceed to build our controllers and models. We will also create endpoints and test the output data using Postman.

The source code to the application in this article is available on Github.

This post was originally posted on Pusher.

Site Footer