Hi all, in this post, we want to learn Slim, It's Microframework from Php, as the name implies this framework is "Slim". We will learn how to install and basic Slim. Slim itself is built with Php language and in slim we are free to set our own folder structure.
What is Slim ?
Slim is one of the microframework of Php. What is Microframework ? , so far we are familiar with frameworks such as Laravel, Codeigneter, Yii, etc. Microframework has a smaller feature than Framework, for example if in Laravel we already have an authentication system, we have a templating system, there is a middleware system, and much more. But if in Microframework like Slim. We will not get such features when we do fresh install. The main feature that we can when fresh install . It's routing system, we can accept request and respone. So it is suitable to make the API, but it is possible to make web application, its advantage if we use Microframework we can mix and match third party. for example in its template system you can use Twig or Blade, in its database system can use Eloquent or PDO regular database.
How to Install Slim?
Before installing Slim, make sure you have installed the composer, if you have not installed the composer please download here https://getcomposer.org/download/.
If you have installed slim, create a folder in this tutorial I create a folder in your Local server like Xampp , Wammp,Laragon or etc, with name of "SLIM". And then open your command prompt in that folder. You can add the version you downloaded with "^ 3.0", or you can remove it to install the most recent version
composer require slim/slim "^3.0"
Open the Text Editor , and We can see that Slim is already there.
We have successfully installed slim.
Basic Routing System in Slim?
We can start making the routing system in Slim by creating the file. in this tutorial I created a file with the name Index.php
Create index.php
<?php
require __DIR__ .'/vendor/autoload.php';
$app = new Slim\App;
$app->run() ;
require __DIR__ .' /vendor/autoload.php ' ;
: Every time we run the application we want to load all the classes in the vendor. so we do not need to load it one by one. This Code usefull for it.
$app = new Slim\App ;
:To use Slim, we will put it in a variable named $app
then with keywords new Slim\App ;
$app->run( ) ;
: To run the Slim we must run with this code.
Add Routing System
We have initialized Slim and have used it and put it in the $app variable. and now we can add code for Route System
<?php
require __DIR__ .' /vendor/autoload.php ';
$app = new Slim\App ;
$app->get( ' / ',function($request,$response ){
return "Hello World !!, I'm Slim";
});
$app->get(' /profil ',function($request,$response){
return "Hello World !!, You're in the Profil";
});
$app->run( );
In index.php. I added code for routing.
$app->get(' / ',function($request,$response ) { } ) ;
: In this code I create a routing with method get( ) , we have 2 parameters. The first parameter is the pathname . The second parameter is a anonymous function, in this function we have two parametes, $request and $response.
return " Hello World " ;
: In this route , i just want to return return Hello World !!, I'm Slim. and see the result of our code.
Add Pretty Url
When fresh install slim, slim does not provide a pretty url, so we must create a pretty url itself.
So when we want to move to another route, then we will get error not found like this.
We must activate the pretty url in slim in the following way :
Create file .htaccess and then insert this code.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

And now you can refresh your localhost , and the error not found will be gone. Finally we have created our own routing system, you can make your routing system as you want. Slim is your Microframework requires a lot of configuration. if you are a person who does not like the fuss for configuration. I suggest you use the framework, but if you like Mix and Match third party library. Maybe you will like slim or other Microframework, so hopefully i hope this tutorial is useful for you
Posted on Utopian.io - Rewarding Open Source Contributors
Free vote for you! Yay!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
thank you :)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
approved !!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hey @alfarisi94 I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit