Creating a Modular Slim Application

Published Feb 17, 2018Last updated Feb 18, 2018
Creating a Modular Slim Application

Introduction

Not long ago I wrote a post, Creating a Modular Koa Application, which is a NodeJS application, but if you are interested in PHP micro frameworks, this is the article for you. You can use any other miro frameworks such as Lumen, I use Slim in this example.

Even though Slim doesn’t come with a database connection utility of its own, you can use any database connection toolkit you like, such as Eloquent, Doctrine DBAL, or Medoo. I also wrote a post on how to use them with Slim sometime ago: Using Eloquent, Doctrine DBAL, and Medoo with Slim 3.

Concept

For example, the modules you may have in your application:

-------------
modules/
    home/
        controllers/
        models/
        views/
    blog/
        controllers/
        models/
        views/
    news/
        controllers/
        models/
        views/
-------------

So, in this example app I have a User module and these are directories and files for it:

-------------
_routes/
    insert_user.php
    update_user.php
    fetch_users.php
    fetch_user.php
    delete_user.php

Controller/
    Controller.php <-- abstract class to be extended by the following classes:
    InsertController.php
    UpdateController.php
    FetchController.php
    DeleteController.php

Gateway/
    Gateway.php <-- abstract class to be extended by the following classes:
    InsertGateway.php
    UpdateGateway.php
    FetchGateway.php
    DeleteGateway.php

Mapper/
    Mapper.php <-- abstract class to be extended by the following classes:
    InsertMapper.php
    UpdateMapper.php
    FetchMapper.php
    DeleteMapper.php

Model/
    Collection.php
    Model.php

index.php <-- the user main route.
-------------

Gluing them up together

Inside the main index.php in User module, let's import all the routes that belong to User:

require '_routes/fetch_users.php';
require '_routes/fetch_user.php';
require '_routes/insert_user.php';
require '_routes/update_user.php';
require '_routes/delete_user.php';

And then in the application route, let's import the User main route in a file called config/routes.php, alongside with other main routes from other modules:

// Add or modify modules to the list here.
return [
    'default/User/',
    ...
    ...
];

The modules in the array above will be imported through the loop in routes.php in the root directory:

// Import all routes from the config.
$modules = require './config/routes.php';
foreach ($modules as $module) {
    require './module/' . $module . 'index.php';
}

Lastly, let's import this routes.php in public/index.php:

...
...

// Register routes.
require 'routes.php';

// Run the application!
$app->run();

You can clone or download this basic modular example from the GitHub repository.

Using the application

  1. To use this application, firstly you need to make sure you have MySQL installed already and imported the database example db.sql that included in the root directory.

After importing the database sample, change the MySQL connection details in config/database.php:

// Database.
$name = 'your-db-name';
$username = 'your-username';
$password = 'your-password';
$host = 'localhost';

return [
    'type' => 'mysql',
    'options' => [
        'PDO::MYSQL_ATTR_INIT_COMMAND' => 'SET NAMES \'UTF8\''
    ],
    'dsn' => 'mysql:dbname=' . $name . ';host=' . $host,
    'host' => $host,
    'name' => $name,
    'username' => $username,
    'password' => $password,
];
  1. Install the application dependencies:
$ composer update
  1. To serve the app at localhost:8181:
$ cd [my-app-name]
$ php -S 0.0.0.0:8181 -t public

After that you can access these HTTP methods below:

1. The GET method

When you visit the app at http://127.0.0.1:8181, you get:

{"status":200,"data":"Hello world!"}

To vist the user index page, use http://127.0.0.1:8181/users, you get:

{"status":200,"data":[]}

Note that the data is empty - "data":[], this is because there is no user added to the user collection in the database yet.

2. The POST method

Now if you go to Google Postman, create the key below and type in the value in the Body section:

Key     Value
--------------------
name    rob
email   rob@example.com

Choose POST method and hit the Send button, you get:

{
    "status": 200,
    "data": {
        "uuid": "5a144fd7-a8ef-3e34-9422-c4fafbd1f627",
        "email": "rob@example.com",
        "name": "rob",
        "createdOn": 1518864426,
        "updatedOn": null
    }
}

When you visit http://127.0.0.1:8181/users again, you get:

{"status":200,"data":[{"uuid":"5a144fd7-a8ef-3e34-9422-c4fafbd1f627","email":null,"name":"rob","createdOn":"17-02-2018 10:47:06 AM","updatedOn":"01-01-1970 01:00:00 AM}]}

You can add more users in and when you just want to query a single user, e.g. http://127.0.0.1:8181/users/rob, you get:

{"status":200,"data":[{"uuid":"5a144fd7-a8ef-3e34-9422-c4fafbd1f627","email":"rob@example.com","name":"rob","createdOn":"17-02-2018 10:47:06 AM","updatedOn":"01-01-1970 01:00:00 AM}]}

3. The PUT method

To update that user, just add the uuid key to Postman:

Key     Value
--------------------
name    robbie
uuid    5a144fd7-a8ef-3e34-9422-c4fafbd1f627

When you hit the Send button with the PUT method, you get:

{
    "status": 200,
    "data": {
        "uuid": "5a144fd7-a8ef-3e34-9422-c4fafbd1f627",
        "email": "robbie@example.com",
        "name": "robbie",
        "createdOn": null,
        "updatedOn": 1518865464
    }
}

4. The DELETE method

Lastly, to delete this user, you just need to provide the uuid key in Postman:

Key     Value
--------------------
uuid     5a144fd7-a8ef-3e34-9422-c4fafbd1f627

When you hit the Send button with the DELETE method, it results:

{
    "status": 200,
    "data": {
        "uuid": "5a144fd7-a8ef-3e34-9422-c4fafbd1f627",
        "email": null,
        "name": null,
        "createdOn": null,
        "updatedOn": null
    }
}

Conclusion

That's it. It is quite easy to develop a modular applications with Slim when you have the route and structure set up, the rest are just icing. Let me know what you think and what is your favourite PHP micro framework for your projects. Any suggestions and mistakes, please leave a comment below. Hope this basic example helps.

Discover and read more posts from LAU TIAM KOK
get started