PHP code example of adt / api-json-router

1. Go to this page and download the library: Download adt/api-json-router library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

adt / api-json-router example snippets


final class RouterFactory
{
    // Prepare your route specification
    public static function getApiRouteSpecification(): array 
    {
		return [
			'/api/devices>POST' => [
				'path' => '/api/devices/<uuid>/request',
				'presenter' => 'DeviceRequest',
				'method' => 'POST',
				'action' => 'create',
				'parameters' => [
					'uuid' => ['type' => 'string', 'RouteList
    {
        $apiRouter = new \ADT\ApiJsonRouter\ApiRouteList('Api');
        $apiRouter->addRoutesBySpecification(self::getApiRouteSpecification());
        return $apiRouter;
    }
}

class DeviceRequestPresenter extends Presenter 
{
    public function actionCreate(string $uuid, string $_type) 
    {
        // Check if you have access to a specific device
        if ($this->deviceQuery->create()->byUuid($uuid)) {
            $this->sendJsonError(404);
        }
    
        // Create a device request
        $deviceRequest = new DeviceRequest($device, $_type)
        $this->em->persist();
        $this->em->flush();
        
        // Send a response
        $this->sendJson(['uuid' => $deviceRequest->getUuid(), 'type' => $device->getType()]);
    }
}

class DocsPresenter extends Presenter
{
	public function actionDefault()
	{
		// Generate API documentation in Markdown format
		$apiDocumentation = new ADT\ApiJsonRouter\ApiDocumentation('API Docs', RouterFactory::getApiRouteSpecification());
		$markdownApiDocumentation = $apiDocumentation->getDocumentation();

		// Generate API documentation in HTML format
		// e.g. you can use https://github.com/erusev/parsedown
		die((new \Parsedown())->text($markdownApiDocumentation));
	}
}

class ErrorPresenter extends Presenter
{
	public function renderDefault(Exception $exception)
	{
		if ($exception instanceof ApiException) {
		    $this->sendJsonError($exception->getCode(), $exception->getMessage());
		}

		Debugger::log($exception, ILogger::EXCEPTION);

		$this->sendJsonError(500, 'There was an error processing your request. Please try again later.');
	}
}