Slim Framework
-
Upload
pramod-raghav -
Category
Software
-
view
197 -
download
5
Transcript of Slim Framework
SlimSLIM IS A PHP MICRO FRAMEWORK THAT HELPS YOU QUICKLY WRITE SIMPLE YET POWERFUL REST API
PRAMOD KUMAR RAGHAV
REST Vs SOAP
REST (Representational State Transfer)
REST Vs SOAP
REST (Representational State Transfer)SOAP (Simple Object Access Protocol)
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.
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.
REST Vs SOAP Cont…
REST is limited by HTTP itself which can’t provide two-phase commit across distributed transactional resources, but SOAP can.
OVERVIEW
Slim is a dispatcher that receives an HTTP request, invokes an appropriate callback routine, and returns an HTTP response. That’s it.
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.
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.
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.
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.
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.
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.
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.
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!
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).
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).
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:
Response HeadersEvery HTTP response has headers. These are metadata that describe the HTTP response but are not visible in the response’s body.
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.
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.