Skip to content

sygic-travel/nette-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tripomatic\NetteApi

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.

About

Build REST APIs with Nette Framework

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published