needcaffeine/slim-api-extras

This package is abandoned and no longer maintained. No replacement package was suggested.

This is a library that adds RESTful capabilities to Slim Framework

1.0.0 2014-05-22 14:34 UTC

This package is not auto-updated.

Last update: 2020-08-21 18:44:22 UTC


README

Latest Stable Version Total Downloads License

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