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/ */
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;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.