PHP code example of russbalabanov / tiny-rest
1. Go to this page and download the library: Download russbalabanov/tiny-rest 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/ */
russbalabanov / tiny-rest example snippets
class ExceptionSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
KernelEvents::EXCEPTION => 'onKernelException',
];
}
public function onKernelException(GetResponseForExceptionEvent $event)
{
$e = $event->getException();
if ($e instanceof ValidationException) {
$error = $this->createValidationMessage($e);
$response = new JsonResponse($error, 400);
$response->headers->set('Content-Type', 'application/problem+json');
$event->setResponse($response);
return $event;
}
return $event;
}
private function createValidationMessage(ValidationException $exception) : string
{
$violation = $exception->getViolationList()->get(0);
return sprintf('%s: %s', $violation->getPropertyPath(), $violation->getMessage());
}
}