2017 © Pedro Peláez
 

library slim-api-extras

This is a library that adds RESTful capabilities to Slim Framework

image

needcaffeine/slim-api-extras

This is a library that adds RESTful capabilities to Slim Framework

  • Monday, May 25, 2015
  • by needcaffeine
  • Repository
  • 1 Watchers
  • 9 Stars
  • 3,897 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Latest Stable Version Total Downloads License, (*1)

Slim API Extras

This library is an extension for the Slim Framework, allowing for easy implementation of APIs with RESTful responses., (*2)

Getting Started

Installation

It's recommended that you install this package via Composer., (*3)

$ 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
    }
}

The Versions

25/05 2015

dev-master

9999999-dev https://github.com/needcaffeine/slim-api-extras

This is a library that adds RESTful capabilities to Slim Framework

  Sources   Download

MIT

The Requires

 

api middleware extensions json rest slim slim framework

22/05 2014

1.0.0

1.0.0.0 https://github.com/needcaffeine/slim-api-extras

This is a library that adds RESTful capabilities to Slim Framework

  Sources   Download

MIT

The Requires

 

api middleware extensions json rest slim slim framework