PHP Restful Slim

From CoderLearner
Jump to: navigation, search

Description

Restful with SLIM framework is a powerful PHP framework to implement Restful Web App.

Requirement

Setup your environment

  • Unzip the Slim package on the directory
  • Make sure it is read/writable, (chmod 755 -R /Slim)

testRest.php

This is a simple Slim restful implementation.

<?php

require 'Slim/Slim.php';

$app = new Slim();

$app->get('/sayhi','sayhi');
$app->get('/sayhi2u/:name','sayhi2u');
$app->run();

function sayhi() {
    echo 'hello world';
}
function sayhi2u($name) {
    echo 'Hi '. $name;
}

?>

.htaccess

This file needs to place at the same directory as the testRest.php. And, this is the rewrite engine for Apache Web Server.

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

Request List

Method                 URL                     Action
====================================================================
GET       http://localhost/testRest.php/sayhi        Display 'hello world'
--------------------------------------------------------------------      
GET       http://localhost/testRest.php/sayhi2u/Paul  Display 'Hi Paul'

Test 1

http://localhost/testRest.php/sayHi

Result

hello world

Test 2

http://localhost/testRest.php/sayHi2u/Bob

Result

Hi Bob

<seo metadescription="PHP Restful Tutorial/PHP Restful Tutorial with SQL" metagenerator="PHP Restful Web Service Tutorial" title="PHP Restful Tutorial" metakeywords="PHP Restful Web Service Tutorial, Restful/MySQL, Restful Foundation, Restful Beginner"/>