PHP code example of wormhit / slim-api

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

    

wormhit / slim-api example snippets


$container = new Api\Module\Container();
 php


ta = array();
//$configData = array(SlimApi\Kernel\Config::ROUTING => 'Api\Module\Routing');
//$configData = ontainer();

$container
    ->setConfig($config->setConfig($configData))
    ->getModule()
    ->run();
 sh
php -S localhost:8000
 php

# src/Module/Routing.php
namespace Api\Module;
use SlimApi\Kernel\Routing as KernelRouting;

class Routing extends KernelRouting
{
    public function init()
    {
        $container = $this->getContainer();
        $slim = $this->getSlim();
        $slim->map(
             '/',
             function() use ($container, $slim) {
                 /** @var \Api\Controller\Index\IndexController $controller */
                 $controller = $container->get('controller.index.index');
                 $slim->response = $controller->getResponse();
             }
        )
        ->via('GET');

        return $this;
    }
}
 php

# src/Module/Container.php
namespace Api\Module;

use SlimApi\Kernel\Container as KernelContainer;

class Container extends KernelContainer
{
    public function initialize()
    {
        parent::initialize();

        $this->initControllers();
    }

    private function initControllers()
    {
        $this['controller.index.index'] = function () {
            return new \Api\Controller\Index\IndexController();
        };
    }
}
 php

# src/Controller/Index/IndexController.php
namespace Api\Controller\Index;

use Slim\Http\Response;

class IndexController
{
    public function getResponse()
    {
        $response = new Response();
        $response->headers->set('Content-Type', 'application/json; charset=utf-8');
        $response->setBody(json_encode(array('apple' => 'green'), JSON_UNESCAPED_UNICODE));
        return $response;
    }
}