Setup your PHP Slim Framework Tutorial

What is Slim Framework? Slim Framework is the most famous php micro framework to create the REST api for your web application or mobile application. REST api is the communication between client and server side in the technology world. Slim framework is very light weight and easy to use to develop REST api according to your needs. I will teach you how to setup your php slim framework and create the REST api in your application. It is just easy.

Prerequisites

1. PHP programming language to perform script

2. SQL query to retrieve data from database

3. XAMPP to run your web server and MySQL

Start your XAMPP

In the first step, you need to run your web server and MySQL to proceed. If you dont know how to use XAMPP go to this link.

Go to your Website path

After you running your XAMPP, go to your xampp directory then go to htdoc folder. Example : C:\xampp\htdocs. After that create a new folder in the htdocs folder for slim project. I name my folder to slim-framework-rest-api.

Install Composer

If you don have php composer, go to install composer in your computer to download dependency in your project. Go to //getcomposer.org/download/ You can either install via .exe or type the command in your cmd.

php -r "copy('//getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === 'e115a8dc7871f15d853148a7fbac7da27d6c0030b848d9b3dc09e2a0388afed865e6a3d6b3c0fad45c48e2b5fc1196ae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php

Open command prompt and Install Slim Framework

Enter the command to navigate to the slim project folder name that you create just now. After that, enter

composer require slim/slim "^3.0"

to install slim framwork in your directory. You can follow the picture below.

download slim in command prompt

Understanding route

Go to the main slim index file in the slim folder, for example my path is C:\xampp\htdocs\slim-framework-rest-api\vendor\slim\slim\example\index.php. The following will route the index.php to / and /hello/{name}.

index.php

$app->get('/', function ($request, $response, $args) {
    $response->write("Welcome to Slim!");
    return $response;
});

$app->get('/hello[/{name}]', function ($request, $response, $args) {
    $response->write("Hello, " . $args['name']);
    return $response;
})->setArgument('name', 'World!');

.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

Access slim home path

Enter local url in your browser for example : //localhost:63343/htdocs/slim-framework-rest-api/vendor/slim/slim/example/.

If you see error such as the bottom pic.

slim-2You may modify index.php

require 'vendor/autoload.php';

to the full path of autoload.php

require 'C:\xampp\htdocs\slim-framework-rest-api\vendor\autoload.php';

Success access slim

After that, access again the path and you will be see the screen such as below.

slim-3

You can also try another route for example for my situation //localhost:63343/htdocs/slim-framework-rest-api/vendor/slim/slim/example/hello/questdot

This route specify in the index.php

Edit Virtual Host

Go to the apache\conf\extra path for example my path is C:\xampp\apache\conf\extra to edit the virtual host file configuration which is “httpd-vhosts.conf. Edit and add the following text in your “httpd-vhosts.conf” file to change your project home path. For my case, my home path for the slim project is C:\xampp\htdocs\slim-framework-rest-api\vendor\slim\slim\example.

<VirtualHost *:80>
DocumentRoot “C:/xampp/htdocs/slim-framework-rest-api/vendor/slim/slim/example”
ServerName slimexample
</VirtualHost>

Edit hosts file

Go to edit your hosts file inside the Windows directory C:\Windows\System32\drivers\etc. Right click the hosts file and edit it in the notepad. If you cannot edit the hosts file, drag the hosts file to your desktop and edit it.

Add this line in your hosts file (127.0.0.1 slimexample)

 

Now, you can access by using this path //slimexample/ in your browser. 

(Visited 5,066 times, 1 visits today)
Advertisements

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

1 Response

  1. October 19, 2016

    […] previous tutorial, you already understand how to setup php slim framework in your computer and also learn how to […]

Leave a Reply

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