needcaffeine / slim-api-extras
This is a library that adds RESTful capabilities to Slim Framework
Installs: 4 148
Dependents: 1
Suggesters: 0
Security: 0
Stars: 9
Watchers: 1
Forks: 4
Open Issues: 0
Requires
- php: >=5.4.0
- slim/slim: >=2.4.2
This package is not auto-updated.
Last update: 2020-08-21 18:44:22 UTC
README
Slim API Extras
This library is an extension for the Slim Framework, allowing for easy implementation of APIs with RESTful responses.
Getting Started
Installation
It's recommended that you install this package via Composer.
$ composer require needcaffeine/slim-api-extras
Usage
<?php require 'vendor/autoload.php'; use \Needcaffeine\Slim\Extras\Views\ApiView; use \Needcaffeine\Slim\Extras\Middleware\ApiMiddleware; // This would probably be loaded from a config file perhaps. $config = array( 'slim' => array( 'debug' => true ) ); // Get the debug value from the config. $debug = $config['slim']['debug']; $app = new \Slim\Slim($config['slim']); $app->view(new ApiView($debug)); $app->add(new ApiMiddleware($debug)); // Example method demonstrating notifications // and non-200 HTTP response. $app->get('/hello', function () use ($app) { $request = $app->request(); $name = $request->get('name'); if ($name) { $response = "Hello, {$name}!"; $data = array("Red" => "dog", "Brown" => "dog"); $response['data'] = $data; } else { $response = array(); $response['notifications'][] = 'Name not provided.'; $responseCode = 400; } $responseCode = $responseCode ?: 200; $app->render($responseCode, $response); }); // Run the Slim application. $app->run();
Example of responses
» curl -i "http://localhost/hello" HTTP/1.1 400 Bad Request Content-Type: application/json; charset=utf-8 { "notifications": "Name not provided.", "meta": { "result": "failure", "status": 400 } } » curl -i "http://localhost/hello?name=Vic" HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8 { "notifications": "Hello, Vic!", "data": { "Red": "dog", "Brown": "dog" }, "meta": { "result": "success", "status": 200 } }