承接 tripomatic/nette-api 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

tripomatic/nette-api

Composer 安装命令:

composer require tripomatic/nette-api

包简介

Build REST APIs with Nette Framework

README 文档

README

Build REST APIs with Nette Framework.

Tripomatic\NetteApi is a very light base for building REST APIs with Nette Framework. It provides an extremely minimalist base presenter, an error presenter that ensures returning a valid JSON on any error and takes care of logging, and a handy JSON prettifier for testing APIs in browser that does not force you to hide your Tracy panel.

Installation

Install Tripomatic\NetteApi using Composer:

$ composer require tripomatic/nette-api

Quickstart

Add Tripomatic\NetteApi extension in your NEON config:

extensions:
  	api: Tripomatic\NetteApi\DI\ApiExtension

Create your first RESTful presenter inherited from minimalist Tripomatic\NetteApi\Application\Presenter:

use Nette\Application\Request;
use Tripomatic\NetteApi\Application\Presenter;
use Tripomatic\NetteApi\Application\Responses\JsonResponse;

class WeatherPresenter extends Presenter
{
	public function get(Request $request)
	{
		$city = $request->getParameter('city');

		$data = ...; // get weather data for the city
		return new JsonResponse($data);
	}

	// implement ohter REST methods similarly
}

Configuration

Tripomatic\NetteApi does not require any additional configuration, it sets up two handy defaults:

  • An error presenter is set up that ensures the API returns a valid JSON in all error situations.
  • API responses are prettified by default when running in debug mode, Tracy panel is also visible.

This behavior can be easily overridden in corresponding extension's section:

api:
	prettify: %debugMode% # can be set to TRUE/FALSE
	errorPresenter: NetteApi:Error
	mapping:
		NetteApi: 'Tripomatic\NetteApi\Application\Presenters\*Presenter'

Bootsraping

If you need some help with writing your application bootsrap here is an example that we use in Tripomatic:

require __DIR__ . '/../vendor/autoload.php';

$tomDebug = getenv('TOM_DEBUG') === 'TRUE' && isset($_COOKIE['tom_api_debug']);
$tomLogDir = getenv('TOM_LOG_DIR') ?: '/var/log/tripomatic/some-api';
$tomTmpDir = getenv('TOM_TMP_DIR') ?: '/var/tmp/some-api';
$tomConfigFile = getenv('TOM_CONFIG_FILE') ?: __DIR__ . '/../config/config.local.neon';

$configurator = new Nette\Configurator;
$configurator->setDebugMode($tomDebug);
$configurator->enableDebugger($tomLogDir);

$configurator->setTempDirectory($tomTmpDir);
$configurator->addConfig(__DIR__ . '/../config/config.neon');
if (file_exists($tomConfigFile)) {
	$configurator->addConfig($tomConfigFile);
}

return $configurator->createContainer();

Yes, with Nette Framework it can really be that minimalist. Everything else can be achieved with Nette's great Dependency Injection implementation. For more details see Nette Framework documentation.

License

Tripomatic\NetteApi is licensed under MIT.

统计信息

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

GitHub 信息

  • Stars: 5
  • Watchers: 7
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-05-25