PHP Web Service Restfull with Slim basic
From w3cyberlearnings
Contents |
Requirement
- Basic knowledge in PHP
- Apache Web Server
- Download Slim http://www.slimframework.com/ Slim
What is Slim?
- Is a simple and powerful PHP framework to create RESTful Web Application
Setup your environment
- Unzip the Slim package on the directory
- Make sure it is read/writable, (chmod 755 -R /Slim)
Create A basic RESTful with Slim (testRest.php)
<?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 file
- The .htaccess file has to place on the same directory as the testRest.php.
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php [QSA,L]
Detail of the testRest.php
Method URL Action ==================================================================== GET http://localhost/testRest.php/sayhi Display 'hello world' -------------------------------------------------------------------- GET http://localhost/testRest.php/sayhi2u/Paul Display 'Hi Paul'
Test 1
- Use sayHi function
http://localhost/testRest.php/sayHi display: hello world
Test 2
- Use sayHi2u function
http://localhost/testRest.php/sayHi2u/Bob display: Hi Bob