PHP code example of needcaffeine / slim-api-extras

1. Go to this page and download the library: Download needcaffeine/slim-api-extras 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/ */

    

needcaffeine / slim-api-extras example snippets




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();