TYPO3 Extbase - Failing to render json via typnum

安稳与你 提交于 2019-12-11 06:16:02

问题


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;

}

}

回答1:


If you inject and use a JsonRepository extbase expexts 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.



来源:https://stackoverflow.com/questions/42471172/typo3-extbase-failing-to-render-json-via-typnum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!