PHP code example of faktore / fe-json-api-utilities
1. Go to this page and download the library: Download faktore/fe-json-api-utilities 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/ */
faktore / fe-json-api-utilities example snippets
use Faktore\FeJsonApiUtilities\Utility\JsonRequestUtility;
class EventsJsonApiController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{
protected JsonRequestUtility $jsonRequestUtility;
// constructor with dependency injection
public function __construct(JsonRequestUtility $jsonRequestUtility)
{
$this->jsonRequestUtility = $jsonRequestUtility;
}
public function searchAction(): ResponseInterface
{
// initialize the request utility
$this->jsonRequestUtility->initialize();
// get JSON POST data and store it in an array
$postData = $this->jsonRequestUtility->parseJsonBody()->getDecodedData();
// ... implement your handling of data
}
}
$postData = $this->jsonRequestUtility->parseJsonBody()->getDecodedData();
use Faktore\FeJsonApiUtilities\Utility\JsonResponseUtility;
class EventsJsonApiController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{
protected JsonResponseUtility $jsonResponseUtility;
// constructor with dependency injection
public function __construct(JsonResponseUtility $jsonResponseUtility)
{
$this->jsonResponseUtility = $jsonResponseUtility;
}
public function searchAction(): ResponseInterface
{
// ... implement your handling of data
// for example get events for a calendar using a repository
// $events = $this->eventRepository->findByDemand($filterDemand);
// convert your result to array first.
// Implementing the convertToArray method will be your responsibility.
// The ConvertUtilities provide a lot of useful functions to help with that.
$events = $this->convertToArray($events);
if ($events) {
// assign data to the response
$this->jsonResponseUtility->assignData('events', $output);
// keep track of success status for the response
$this->jsonResponseUtility->setSuccess(true);
} else {
// add an error
$this->jsonResponseUtility->addError('no events found');
// keep track of success status for the response
$this->jsonResponseUtility->setSuccess(false);
}
// ... do more checks and validations to add errors if necessary.
// Lastly, make sure to return a PSR-compliant response. The JsonResponse works just fine in Controller context.
return $this->jsonResponse(
$this->jsonResponseUtility->getOutput()
)->withStatus(200);
}
}