wormhit/slim-api 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

wormhit/slim-api

Composer 安装命令:

composer require wormhit/slim-api

包简介

PHP API framework based on slim and pimple

README 文档

README

API framework based on slim and pimple

Build Status Scrutinizer Quality Score Coverage Status Latest Stable Version License

Installation

Create composer.json file

{
    "require": {
        "wormhit/slim-api": "dev-master"
    },
    "autoload": {
        "psr-4": {"Api\\": "src/"}
    }
}

Download composer and run php composer.phar install

Execution

Application can be quickly started by using php built in web server.

Index

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

Server

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.

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
# 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.

Container

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

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.

Testing

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.

统计信息

  • 总下载量: 557
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2014-03-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固