JSON Format

1
2
3
4
5
6
demo_clients-test:
   path:         api/demo/clients/test
   controller:   LMS\Demo\Controller\ClientApiController::test
   format:       json
   defaults:
     plugin:     ClientApi

Direct Output

<?php
    declare(strict_types = 1);

    namespace LMS\Demo\Controller;

    use Psr\Http\Message\ResponseInterface;

    class ClientApiController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
    {
        public function testAction(): ResponseInterface
        {
           return $this->jsonResponse(json_encode([
               'status' => 'ok',
           ]));
        }
    }

Custom View

Instead of Direct Output, it’s possible to create individual view for building the output. In the current example, Classes/View/ClientApi/TestJson.php: expected to be in place.

<?php
    declare(strict_types = 1);

    namespace LMS\Demo\View\ClientApi;

    class TestJson extends \TYPO3\CMS\Extbase\Mvc\View\AbstractView
    {
        public function render()
        {
            return json_encode(['status' => 'ok']);
        }
    }

Custom Template

Or even alternatively, Resources/Private/Templates/ClientApi/Test.json: expected to be in place.

Warning

$defaultViewObjectName should not be overwritten.

{
  "status": "ok"
}

Example request

Note

Accept: application/json

Content-Type: application/json

Headers are recommended.

curl --location --request GET 'https://demo.ddev.site/api/demo/clients/test' \
    --header 'Content-Type: application/json' \
    --header 'Accept: application/json'