TYPO3 Extbase-无法通过typnum渲染json [英] TYPO3 Extbase - Failing to render json via typnum
问题描述
TYPO3 Extbase-无法通过typnum渲染json
TYPO3 Extbase - Failing to render json via typnum
在list/edit/new/remove动作旁边(有效),我尝试将输出呈现为json.但是,没有价值可呈现.如果我做的简单...
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);
它确实返回...
{"value":"001"}
我想念什么?
通过使用并引用相同的存储库,其工作原理:
With using and referencing to the same repository its working:
JSONController.php
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
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;
}
}
推荐答案
如果注入并使用JsonRepository
extbase,则会扩展名为Json
的域对象.如果只想将现有的域对象呈现为其JSON表示形式,则只需使用在listAction()
和detailAction()
中使用的相同存储库即可.
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()
.
看看我的示例: https://usetypo3.com/json-view.html
此外,像在存储库中一样,在return
之后进行的调试也将永远不会执行.
Also, a debug after the return
like you did in your repository will never be executed.
这篇关于TYPO3 Extbase-无法通过typnum渲染json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!