tyea/slender

A PHP front controller for RESTful APIs

1.5.0 2019-08-24 15:36 UTC

README

About

Slender is a PHP front controller for RESTful APIs.

Features

  • PSR-7 request and response objects
  • Routing and dispatch components
  • Wildcard routes
  • PSR-15 middlewares
  • PSR-11 dependency injection container
  • Dependency injection autowiring

Requirements

  • PHP 7.2 with the JSON extension
  • Apache 2.4 with the mod_rewrite module

Configuration

AllowOverride all
Options FollowSymLinks

Installation

  • composer require tyea/slender
  • ln -sr vendor/tyea/slender/public public

Examples

<?php

const SERVICES = [
	["Interfaces\\ExampleInterface", "ServiceProviders\\ExampleServiceProvider::provide", TRANSIENT] // SINGLETON/TRANSIENT
];
const ROUTES = [
	["POST", "/examples", "Controllers\\ExampleController::createAction"],
	["GET", "/examples/{id}", "Controllers\\ExampleController::readAction"]
];
const MIDDLEWARES = [
	"Zend\\Expressive\\Helper\\BodyParams\\BodyParamsMiddleware",
	"ExampleMiddleware"
];
const EXCEPTION_HANDLER = "ExceptionHandler::handle";
<?php

namespace ServiceProviders;

use League\Container\Container;
use Services\ExampleService;

class ExampleServiceProvider
{
	public function provide(Container $container): ExampleService
	{
		return $container->get("Services\\ExampleService");
	} 
}
<?php

namespace Controllers;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Response\JsonResponse;
use Interfaces\ExampleInterface;

class ExampleController
{
	private $example = null;
	
	public function __construct(ExampleInterface $example)
	{
		$this->example = $example;
	}
	
	public function createAction(RequestInterface $request): ResponseInterface
	{
		$data = $request->getParsedBody();
		$id = $this->example->create($data);
		$example = $this->example->findById($id);
		return new JsonResponse($example, 200);
	}
	
	public function readAction(RequestInterface $request): ResponseInterface
	{
		$id = (int) $request->getAttribute("id");
		$example = $this->example->findById($id);
		return new JsonResponse($example, 200);
	}
}
<?php

use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ResponseInterface;

class ExampleMiddleware implements MiddlewareInterface
{
	public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
	{
		return $handler->handle($request);
	}
}
<?php

use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Response\JsonResponse;

class ExceptionHandler
{
	public function handle(Exception $exception): ResponseInterface
	{
		switch (get_class($exception)) {
			case "League\\Route\\Http\\Exception\\NotFoundException":
				return new JsonResponse((object) [], 404);
			case "League\\Route\\Http\\Exception\\MethodNotAllowedException":
				return new JsonResponse((object) [], 405);
			default:
				return new JsonResponse((object) [], 500);
		}
	}
}

Author

Written by Tom Yeadon.