API framework based on slim and pimple
Create composer.json file
{
"require": {
"wormhit/slim-api": "dev-master"
},
"autoload": {
"psr-4": {"Api\\": "src/"}
}
}
Download composer and run php composer.phar install
Application can be quickly started by using php built in web server.
Before starting server edit your index.php file
<?php
require 'vendor/autoload.php';
$configData = array();
//$configData = array(SlimApi\Kernel\Config::ROUTING => 'Api\Module\Routing');
//$configData = include 'config/config.php';
$config = new SlimApi\Kernel\Config();
$container = new SlimApi\Kernel\Container();
//$container = new Api\Module\Container();
$container
->setConfig($config->setConfig($configData))
->getModule()
->run();
Start application by executing command from terminal
php -S localhost:8000
and point your browser to http://localhost:8080
Initially application will return decorated 404 page.
Set up routing by uncommenting $configData = array(..) line in index.php. Then create new routing class.
Namespace \Api and path src/ was set up in your composer.json
<?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;
}
}
This setup will tell slim framework to match "/" request. When request is matched, appropriate closure function will be executed.
Usually closure will create controller and get response object from it.
SlimApi is using pimple to keep objects in one place.
In this case, code is asking container for 'controller.index.index' class.
Create custom container by extending SlimApi\Kernel\Container.
Uncomment line in index.php
$container = new Api\Module\Container();
and create custom container class
<?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();
};
}
}
Now your script will be able to find 'controller.index.index' key.
Still, now controller class in closure will not be found, because it dose not exist yet.
Controller usually should return Slim\Http\Response object. This response will then be handled by custom routing. In this case Api\Module\Routing.
Create index controller
<?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;
}
}
Now refresh http://localhost:8080 and you should see json response.
You can test simple-api files using command
./vendor/bin/phpunit -c vendor/wormhit/slim-api/tests/phpunit.xml
Library is really simple and easy to understand. If things dont work out as expected, check terminal output when running php server.