Saturday, 15 October 2016

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.

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

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

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..

Featured post

How to create dynamic schema in mongodb

Sometime what we need in our application is to create mongodb schema for collection and some of our collection schema are same but names are...

Popular Posts