2017 © Pedro Peláez
 

library slim-api

PHP API framework based on slim and pimple

image

wormhit/slim-api

PHP API framework based on slim and pimple

  • Friday, November 14, 2014
  • by wormhit
  • Repository
  • 3 Watchers
  • 1 Stars
  • 548 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

SlimApi

API framework based on slim and pimple, (*1)

Build Status Scrutinizer Quality Score Coverage Status Latest Stable Version License, (*2)

Installation

Create composer.json file ``` json { "require": { "wormhit/slim-api": "dev-master" }, "autoload": { "psr-4": {"Api\": "src/"} } }, (*3)


Download [composer][3] and run php composer.phar install ## Execution Application can be quickly started by using [php built in web server][4]. ### Index Before starting server edit your index.php file ``` php <?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();

Server

Start application by executing command from terminal, (*4)

``` sh php -S localhost:8000, (*5)


and point your browser to [http://localhost:8080][5] Initially application will return decorated 404 page. ### Routing 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 <?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., (*6)

Usually closure will create controller and get response object from it., (*7)

SlimApi is using pimple to keep objects in one place., (*8)

In this case, code is asking container for 'controller.index.index' class., (*9)

Container

Create custom container by extending SlimApi\Kernel\Container., (*10)

Uncomment line in index.php, (*11)

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

and create custom container class, (*12)

``` php <?php, (*13)

src/Module/Container.php

namespace Api\Module;, (*14)

use SlimApi\Kernel\Container as KernelContainer;, (*15)

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

    $this->initControllers();
}

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

}, (*17)


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 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 <?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][5] and you should see json response., (*18)

Testing

You can test simple-api files using command sh ./vendor/bin/phpunit -c vendor/wormhit/slim-api/tests/phpunit.xml, (*19)

Library is really simple and easy to understand. If things dont work out as expected, check terminal output when running php server., (*20)

The Versions

14/11 2014

dev-master

9999999-dev https://github.com/andrejsstepanovs/slim-api

PHP API framework based on slim and pimple

  Sources   Download

Apache-2.0

The Requires

 

api framework slim

17/05 2014

v1.0.1

1.0.1.0 https://github.com/wormhit/slimApi

PHP API framework based on slim and pimple

  Sources   Download

Apache-2.0

The Requires

 

api framework slim

16/03 2014

v1.0.0

1.0.0.0 https://github.com/wormhit/slimApi

php api bootstrap project using slim for routing and pimple for dependency injection container

  Sources   Download

Apache-2.0

The Requires

 

api framework slim