REST Introduction REST Concept REST is Representational State

  • Slides: 13
Download presentation
REST Introduction

REST Introduction

REST Concept REST is • Representational State Transfer between Resource • A style of

REST Concept REST is • Representational State Transfer between Resource • A style of software architecture • A Virtual state-machine A network of web pages (a virtual state-machine), where the user progresses through an application by selecting links (state transitions), resulting in the next page (representing the next state of the application) being transferred to the user and rendered for their use.

REST Data Elements • Resources and Resource Identifiers • Uniform Interface (GET, PUT, POST,

REST Data Elements • Resources and Resource Identifiers • Uniform Interface (GET, PUT, POST, DELETE) • Resource Oriented HTTP Method CRUD Desc. POST CREATE Create - GET RETRIEVE Retrieve Safe, Idempotent, Cacheable PUT UPDATE Update Idempotent DELETE Delete Idempotent

REST Data Elements Representations • HTML / XML / images / sounds / …

REST Data Elements Representations • HTML / XML / images / sounds / …

REST V. S. SOAP • Simple Object Access Protocol • RPC protocol that go

REST V. S. SOAP • Simple Object Access Protocol • RPC protocol that go through firewalls • Communication protocol between applications • A format for sending messages

REST V. S. SOAP REST • “The Web is the universe of globally accessible

REST V. S. SOAP REST • “The Web is the universe of globally accessible information” • Resource oriented • User-driven interactions via forms • Few operations (generic interface) on many resources • URI: Consistent naming mechanism for resources • Focus on scalability and performance of large scale distributed hypermedia systems SOAP • “The Web is the universal transport for messages” • Activity/Service oriented • Orchestrated reliable event flows • Many operations (service interface) on few resources • Lack of standard naming mechanism • Focus on design of integrated (distributed) applications

REST V. S. SOA Two of most common styles of use of Web Services

REST V. S. SOA Two of most common styles of use of Web Services • Service-oriented architecture • “Message oriented” (SOAP) • Contract provided by WSDL • REST • Focus on interacting with stateful resources, rather than messages or operations.

Slim Framework http: //www. slimframework. com/

Slim Framework http: //www. slimframework. com/

Slim Framework Slim is a PHP micro framework that helps you quickly write simple

Slim Framework Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs. One of the features – powerful routing • REST URI

Slim Framework Install in c: xampphtdocsslim Access: http: //localhost/slim/ Apache. htaccess (to allow routing

Slim Framework Install in c: xampphtdocsslim Access: http: //localhost/slim/ Apache. htaccess (to allow routing access in the index. php) <If. Module mod_rewrite. c> <If. Module mod_negotiation. c> Options -Multi. Views </If. Module> Rewrite. Engine On # Redirect Trailing Slashes. . . Rewrite. Rule ^(. *)/$ /$1 [L, R=301] # Handle Front Controller. . . Rewrite. Cond %{REQUEST_FILENAME} !-d Rewrite. Cond %{REQUEST_FILENAME} !-f Rewrite. Rule ^ index. php [L] </If. Module>

Slim Framework REST Hello World require 'Slim/Slim. php'; Slim: : register. Autoloader(); $app =

Slim Framework REST Hello World require 'Slim/Slim. php'; Slim: : register. Autoloader(); $app = new Slim(); //REST routing with inline function $app->get('/hello/: name', function ($name) { echo "Hello, $name"; }); In browser url: http: //localhost/slim/hello/jb In browser output: Hello, jb

Slim Framework REST Calculator function do. Sum($num 1, $num 2) { $total = $num

Slim Framework REST Calculator function do. Sum($num 1, $num 2) { $total = $num 1 + $num 2; echo "$num 1 + $num 2 = $total"; } //REST routing with external function and 2 parameters //Sample link: http: //localhost/slim/calc/2/3 $app->get('/calc/: num 1/: num 2', 'do. Sum'); In browser url: http: //localhost/slim/calc/2/3 In browser output: 2 + 3 = 5

Slim Framework Returning JSON function do. Sum. Json($num 1, $num 2) { $total =

Slim Framework Returning JSON function do. Sum. Json($num 1, $num 2) { $total = $num 1 + $num 2; $calc = new Calc(); $calc->num 1 = $num 1; $calc->num 2 = $num 2; $calc->total = $total; echo json_encode($calc); } //Returning a json $app->get('/calcjson/: num 1/: num 2', 'do. Sum. Json'); In browser url: http: //localhost/slim/calcjson/2/3 In browser output: {"num 1": "2", "num 2": "3", "total": 5}