PHP code example of voku / slim-json-api

1. Go to this page and download the library: Download voku/slim-json-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

voku / slim-json-api example snippets


      $app = new \Slim\Slim();

    $app->view(new \voku\slim\JsonApiView());
    $app->add(new \voku\slim\JsonApiMiddleware());

$app->get('/', function() use ($app) {
  $app->render(
      200,
      array(
          'msg' => 'welcome to my API!',
      )
  );
});

$app->get('/user/:id', function($id) use ($app) {

  // your code here

  $app->render(
    404,
    array(
        'error' => TRUE,
        'msg'   => 'user not found',
    )
  );
});

$app->get('/user/:id', function($id) use ($app) {

  // your code here

  if (...) {
    throw new Exception("Something wrong with your request!");
  }
});



$app = new \Slim\Slim();

$app->view(new \voku\slim\JsonApiView("data", "meta"));
$app->add(new \voku\slim\JsonApiMiddleware());

function apiRequest() {
  $app = \Slim\Slim::getInstance();
  $app->view(new \voku\slim\JsonApiView());
  $app->add(new \voku\slim\JsonApiMiddleware());
}

$app->get('/home', function() use ($app){
  // regular html response
  $app->render("template.tpl");
});

$app->get('/api', 'apiRequest', function() use ($app){
  // this request will have full json responses

  $app->render(
    200,
    array(
        'msg' => 'welcome to my API!',
    )
  );
});