slim framework

21
Slim SLIM IS A PHP MICRO FRAMEWORK THAT HELPS YOU QUICKLY WRITE SIMPLE YET POWERFUL REST API PRAMOD KUMAR RAGHAV

Upload: pramod-raghav

Post on 14-Apr-2017

189 views

Category:

Software


5 download

TRANSCRIPT

Page 1: Slim Framework

SlimSLIM IS A PHP MICRO FRAMEWORK THAT HELPS YOU QUICKLY WRITE SIMPLE YET POWERFUL REST API

PRAMOD KUMAR RAGHAV

Page 2: Slim Framework

REST Vs SOAP

REST (Representational State Transfer)

Page 4: Slim Framework

REST Vs SOAP

REST (Representational State Transfer)SOAP (Simple Object Access Protocol)

RESTs is good when you are exposing a public API over the internet to handle CRUD operations on data. REST is focused on accessing named resources through a single consistent interface.

SOAP brings it’s own protocol and focuses on exposing pieces of application logic (not data) as services.

Pramod Kumar Raghav
Pramod Kumar Raghav
SOAP exposes operations. SOAP is focused on accessing named operations, each implement some business logic through different interfaces.
Page 5: Slim Framework

REST Vs SOAP Cont…

REST permits many different data formats like (JSON, XML, CSV,RSS …)SOAP only permits XML data formats.

REST allows better support for browser clients due to it’s support for JSON.

REST reads can be cached, SOAP based reads cannot be cached.

SOAP support ACID Transactions over a service while REST isn’t ACID compliant.

Page 6: Slim Framework

REST Vs SOAP Cont…

REST is limited by HTTP itself which can’t provide two-phase commit across distributed transactional resources, but SOAP can.

Pramod Kumar Raghav
Internet apps generally don’t need this level of transactional reliability, enterprise apps sometimes do.
Page 7: Slim Framework

OVERVIEW

Slim is a dispatcher that receives an HTTP request, invokes an appropriate callback routine, and returns an HTTP response. That’s it.

Page 8: Slim Framework

Installation

System Requirementso Web server with URL rewritingo PHP 5.5 or newer

How to Install SlimInstall Slim with Composer and execute  the below command under your project root directory.composer require slim/slim "^3.0"

This command downloads the Slim Framework and its third-party dependencies into your project’s vendor/ directory.

Page 9: Slim Framework

Application

The Application, (or Slim\App) is the entry point to your Slim application and is used to register the route that link to your callbacks or controllers.

The Application, (or Slim\App) is the entry point to your Slim application and is used to register the route that link to your callbacks or controllers.

Page 10: Slim Framework

Application Configuration

The Application accepts just one argument. This can be either a Container instance or an array to configure the default container that is created automatically.

The Application, (or Slim\App) is the entry point to your Slim application and is used to register the route that link to your callbacks or controllers.

Page 11: Slim Framework

Request & ResponseSlim app’s  routes and middleware are given a PSR 7 request object that represents the current HTTP request received by your web server.

The Application, (or Slim\App) is the entry point to your Slim application and is used to register the route that link to your callbacks or controllers.

The PSR 7 request object is injected into your Slim application routes as the first argument to the route callback.

The PSR 7 response object is injected into your Slim application routes as the second argument to the route  callback.

Page 12: Slim Framework

Request & ResponseThe Application, (or Slim\App) is the entry point to your Slim application and is used to register the route that link to your callbacks or controllers.

Page 13: Slim Framework

The Request Headers

Every HTTP request has headers.

These are metadata that describe the HTTP request but are not visible in the request’s body.

The Application, (or Slim\App) is the entry point to your Slim application and is used to register the route that link to your callbacks or controllers.

Page 14: Slim Framework

The Request Headers ….Get All Headers: You can fetch all HTTP request headers as an associative array.

The Application, (or Slim\App) is the entry point to your Slim application and is used to register the route that link to your callbacks or controllers.

Page 15: Slim Framework

The Request Headers ….Get One Header: You can get a single header’s value(s) with the PSR 7 Request object’s getHeader($name) method. This returns an array of values for the given header name. Remember, a single HTTP header may have more than one value!

Page 16: Slim Framework

The Request BodyFollowing methods have request body

POST, PUT, PATCHSlim can parse JSON, XML, and URL-encoded data out of the box.

JSON requests are converted into associative arrays with json_decode($input, true).

XML requests are converted into a SimpleXMLElement with simplexml_load_string($input).

URL-encoded requests are converted into a PHP array with parse_str($input).

Page 17: Slim Framework

The Request BodyYou can get the HTTP request body StreamInterface instance with the PSR 7 Request object’s getBody() method. The getBody() method is preferable if the incoming HTTP request size is unknown or too large for available memory.

JSON requests are converted into associative arrays with json_decode($input, true).

XML requests are converted into a SimpleXMLElement with simplexml_load_string($input).

URL-encoded requests are converted into a PHP array with parse_str($input).

Page 18: Slim Framework

Response StatusEvery HTTP response has a numeric status code. The status code identifies the type of HTTP response to be returned to the client. 

You can copy a PSR 7 Response object and assign a new status code like this:

Page 19: Slim Framework

Response HeadersEvery HTTP response has headers. These are metadata that describe the HTTP response but are not visible in the response’s body. 

Page 20: Slim Framework

Response StatusSet Header: You can set a header value with the PSR 7 Response object’s withHeader($name, $value) method.

Append Header: You can append a header value with the PSR 7 Response object’s withAddedHeader($name, $value) method.

Remove Header: You can remove a header with the Response object’s withoutHeader($name) method.

Page 21: Slim Framework

Returning JSONSlim’s Response object has a custom method withJson($data, $status, $encodingOptions) to help simplify the process of returning JSON data.

JSON data can be returned with a default 200 HTTP status code. But we can also return JSON data with a custom HTTP status code.