Getting started with slim framework 3 of PHP

Slim is a micro framework that helps you create a simple web applications and api's. It comes with the middleware architecture and url dispatcher that makes it ideal for static websites or API prototyping. It supports all(GET, POST, PUT, DELETE) the HTTP methods. It requires PHP 5.5.0 or newer.

Slim framework


In this article i will show you how to install Slim Framework. It is extremely easy to setup, thanks to composer support.

Step 1: Installation 


# go to your projects directory
$ cd var/www/html
 
# create new project directory 
$ mkdir myslimapp
 
#change into the App directory
$ cd myslimapp
 
# install slim via composer
$ composer require slim/slim "^3.0"

Above issued composer command will install Slim and all required dependencies.Now create a index.php to say hello.

Step 2: Create a action which print hello.


<?php
require 'vendor/autoload.php';
$app = new Slim\App();
$app->get('/hello/{name}', function ($request, $response, $args) {
    $response->write("Hello Slim , " . $args['name']);
    return $response;
});
$app->run();

Step 3: Go to browser and type  http://localhost/myslimapp/hello/john
OR
without .htaccess
http://localhost/myslimapp/index.php/hello/john

If you want to remove index.php from the URL, then change the apache configurations with .htaccess. The .htaccess file should look like this.


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

Check the permission for index.php and .htaccess.Please feel free to comment below your suggestion and problems..

No comments:

Post a Comment

Download the eBook of react js for beginners

ReactJS basically is an open-source JavaScript library which is used for building user interfaces specifically for single page applications...

Popular