bdemirkir/slim-json-view

Slim Framework 3 view helper for simple JSON API's

0.1.1 2015-12-24 11:04 UTC

This package is auto-updated.

Last update: 2024-12-05 00:43:31 UTC


README

This is a very simple Slim Framework view helper for JSON API's. You can use this component to create simple JSON responses in your Slim Framework application.

Install

Via Composer

$ composer require bdemirkir/slim-json-view

Requires Slim Framework 3 and PHP 5.5.0 or newer.

Usage

// Create Slim app
$app = new \Slim\App();

// Fetch DI Container
$container = $app->getContainer();

// Register JSON View helper
$container['view'] = function ($c) {
    return new \Slim\Views\JsonView();
};

// Successful response
$app->get('/hello/{name}', function ($request, $response, $args) {
    return $this->view->render($response, [
        'name' => $args['name']
    ]);
});

// Unauthorized response
$app->get('/unauthorized/{name}', function ($request, $response, $args) {
    return $this->view->render($response, [
        'name' => $args['name']
    ], 401);
});

// Run app
$app->run();

License

The MIT License (MIT). Please see License File for more information.