Site icon WDB24

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

slim3 featured image

If you are a PHP developer for some time now, then you already heard about Slim framework. Slim is mainly popular for building APIs. Reason is its routing feature but you can also develop dynamic websites and systems using slim.

As per official definition:

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.

Features of Slim Framework:

There are four main features of slim.

I am not going to cover API in this tutorial. I will do that in my upcoming tutorials. But right now I am going to develop a website using slim 3 framework.

This will be the series of tutorials because I cannot cover all in one tutorial. But I can share you what I will going to do in this tutorial.


Slim 3 Framework Tutorial:

If you don’t have composer on your machine. Then you can check my post How to install Composer on Windows with XAMPP.

I am using xampp for my local development so I am going to create folder in htdocs as slimnew. Now open command prompt (ctrl +r and type cmd) go to slimnew folder under htdocs and type below command.

When you hit enter then slim installation will start and you will see below messages.

Slim 3 initial Folder Structure:

By default installation create only vendor folder and composer .json and composer.lock file. Just like below

Download Views and Templates:

Slim does not have a view layer by default because slim view is the http response. But slim provides the Twig-View and PHP-View components to render templates.

I am going to use PHP-View for templating. Type below command under htdocs/slimnew

Create Src Folder:

Create Settings.php:

Now create src folder under slimnew and create file settings.php and put below code in settings.php

Above code is just an array with settings index as first and displayErrorDetails, addContentLengthHeader and db indexes as second with their respective values. I will cover database in my next tutorial.

Create routes.php

Slim provides 8 routes for handling HTTP request. We will cover them according to the requirement of this tutorial. But if you want to look them. You can visit Slim Router.

In the above routes.php code:

Create index.php

Create index.php on folder root htdocs/slimnew/index.php. Paste below code.

In the above of index.php on top we used:


Create Templates Folder:

Homepage.php:

In the above code we print $main_heading as main heading and $sub_heading as paragraph. Both $main_heading and $sub_heading are passed as an argument in src/routes.php file.

About.php:

Create layouts folder:

In templates/layouts folder create 2 files header.php and footer.php

header.php

Above is the header code which include css files and render top navigation. $baseUrl is a variable which we set in index.php in view container.

footer.php

Assets Folder:

In this folder we put js and css files.

Final Slimnew Folder Structure:

All things are done now it’s time to check slimnew in browser. We have 2 options for open site in browser first one is goto browser and type slimnew url (for me it is localhost:8080/slimnew) and second method is goto command prompt xampp/htdocs/slimnew and type php -S localhost:9000 and then goto browser and type localhost:9000.

If you are using second option then must change baseUrl in index.php right now it is set to /slimnew/ you have to change this with "/".

Second tutorial on this series is Slim 3 Framework Tutorial: Create, Configure, Load and Access Database

Exit mobile version