kartenmacherei/rest-framework

Micro framework for creating RESTful webservices

3.0.1 2023-10-09 10:32 UTC

README

Scrutinizer Code Quality Build Status SensioLabsInsight

The goal of this framework is to enable us to quickly bootstrap new RESTful Services while sticking to our very strict coding guidelines. This especially means that any kind of magic should be avoided.

Basic Concepts

Components

RestResource

  • Provides a Pattern that can be matches against an URI
  • Supports HTTP verbs by implementing interfaces like SupportsGetRequests
  • Returns Action objects through explicit methods like getPostCommand() or getQuery()

ResourceRouter

  • Holds references to all RestResource objects it is responsible for
  • Determines if it is responsible for routing a given URL in canRoute()
  • Returns a RestResource in doRoute()

Command

  • Changes the state of a resource (like creating or updating)

Query

  • Does not change the state of a resource and only returns existing data.

Using the Framework

Requirements

  • Composer
  • PHP 7.0+

Add the Framework to your composer.json:

	"require": {
		"kartenmacherei/rest-framework": "dev-master"
	}

Connect your code to the Framework:

// create a request
$request = Request::fromSuperGlobals();

// create config object
// 'app-name' will be used as newrelic appname, if monitoring was enabled
// bool $enableMonitoring if true, framework will set newrelic transaction name based on mapping
// array $transactionMapping, class name to transaction name mapping array for each action. If action was not set, fallback is transaction_name_was_not_set
$config = new Config('app-name', $enableMonitoring, $transactionNamesMapping);

// create a new instance of the framework
$framework = Framework::createInstance($config);

// register a RestResource Router
$framework->registerResourceRouter(new BasketResourceRouter());

// let the framework process the request
$response = $framework->run($request);

// send the response to the client
$response->flush();

See https://github.com/kartenmacherei/rest-framework-example for a working example.

License

This software is licensed under the terms of the MIT license. See LICENSE.md for the full license.