typo3extbasetypo3-7.6.x

TYPO3 Extbase - Failing to render json via typnum


TYPO3 Extbase - Failing to render json via typnum

Next to list/edit/new/remove action (which work) I tried to render the output in json. But no values render. If I do a simple ...

$data = array('value'=>'001');
return json_encode($data);

It does return ...

{"value":"001"}

What am I missing?

Edit: With using and referencing to the same repository its working:

JSONController.php

<?php
namespace Vendor\Lei\Controller;
use Vendor\Lei\Domain\Model\Lei;

/**
 * JSONController
 */
 class JSONController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {

/**
 * leiRepository
 *
 * @var \Vendor\Lei\Domain\Repository\LeiRepository
 * @inject
 */
protected $leiRepository;

/**
 * @var string
 */
protected $defaultViewObjectName = 'TYPO3\CMS\Extbase\Mvc\View\JsonView';

/**
 * action jsonRequest
 *
 * @return void
 */
public function jsonRequestAction() {

    //$data = array('value'=>'001');
    //return json_encode($data);

    $this->view->setVariablesToRender(array('records'));
    $this->view->assign('records', $this->leiRepository->jsonRequest());

}           

}

LeiRepository.php

<?php
namespace Vendor\Lei\Domain\Repository;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;

class LeiRepository extends \TYPO3\CMS\Extbase\Persistence\Repository {

...

public function jsonRequest() {

    $query = $this->createQuery();
    $result = $query->setLimit(100)->execute();
    return $result;

}

}

Solution

  • If you inject and use a JsonRepository extbase expects a domain object called Json. If you just want to render already existing domain objects as their JSON representation, just use the same repositories you used in your listAction() and detailAction().

    Have a look at my example: https://usetypo3.com/json-view.html

    Also, a debug after the return like you did in your repository will never be executed.