jgautheron/slim-jsend

JSend implementation for the Slim PHP framework

dev-master 2015-04-01 15:10 UTC

This package is auto-updated.

Last update: 2024-04-05 18:07:43 UTC


README

JSend middleware for the Slim PHP framework.

How to install

You can install SlimJsend with Composer by:

composer install jgautheron/slim-jsend

or adding this line to your composer.json file:

"jgautheron/slim-jsend": "dev-master"

How to use

require 'vendor/autoload.php';
$app = new \Slim\Slim();

// Add the middleware globally
$app->add(new \SlimJsend\Middleware([
    // true means *
    'cors' => true,
    // for a finer control over the allowed origin
    'cors' => 'https://foo.com'
]));

$app->get('/', function() use ($app) {
    // SlimJsend will automatically generate the proper JSend response depending of the status code
    $app->render(200, $messages); // success
    $app->render(500, $data); // fail
    
    // if an exception is thrown, it will be automatically converted to a JSend error message
    throw new Exception('Uh oh... missing username');
});

$app->run();

Credits