Slim 3 Framework Tutorial: Create, Configure, Load and Access Database

This is the second article of the slim 3 framework tutorial. In my previous article we have successfully downloaded slim 3 framework and worked with routes and templates.

If you directly land on this page just to understand how to access database in slim 3 then this tutorial is for you. But if you want to develop website using slim 3 then please also read my previous post for better understanding.

Also read: Slim 3 Framework Tutorial: Download, Setup, Create Routes and Template

In this slim 3 framework tutorial I am going to configure and access database. We already created home page and about page in our previous post. In this post I will create blog listing page and show records from database.


Create, Configure, Load and Access Database in Slim 3 Framework:

Following are the step by step process of how we will create blog page.

  • Create database as demo
  • Create database table as blog with 5 fields (id, title, blog_content, author, created).
  • Create db index with host, user, password and database name in /src/settings.php
  • Create database connection index.php using
  • Create route for the blog page in /src/routes.php.
  • Get records from database by using query in blog route.
  • Pass fetched record to the front template and display them using loop.

slim 3 framework tutorial database

Create Database:

Create Database Table:

Set Database Variable in settings.php:

Database Connection in index.php:

This is a beginner tutorial so I am using mysqli class to access database but it is better to use PDO.

Create Route for Blog:

View Blog.php:

Blog.php file is located in templates folder.

 

2 Replies to “Slim 3 Framework Tutorial: Create, Configure, Load and Access Database”

Leave a Reply

Your email address will not be published. Required fields are marked *