TYPO3 Extbase-无法通过typnum渲染json [英] TYPO3 Extbase - Failing to render json via typnum

查看:84
本文介绍了TYPO3 Extbase-无法通过typnum渲染json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆