2017-02-26 88 views
1

TYPO3 Extbase渲染JSON - 未能通過typnumTYPO3 Extbase - 未能通過typnum

下一頁渲染JSON列出/編輯/新/刪除操作我試圖呈現在J​​SON輸出(這工作)。但沒有值呈現。如果我做一個簡單的...

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

它返回......

{"value":"001"} 

我缺少什麼?

編輯:隨着使用和引用同一倉庫的工作:如果你注入並使用JsonRepository

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; 

} 

} 
+0

你的'jsonReturn()'方法返回任何東西嗎?我想'setVariablesToRender()'不期望一個數組,但我現在還不確定。我在這裏寫了一篇關於JSON視圖的文章:https://usetypo3.com/json-view.html – Daniel

+0

我正在看它:-)我如何檢查jsonReturn()是否返回任何內容?我試圖轉儲(var_dump($結果);)它在JsonRepository.php中,並看着typnum ...但沒有。我或多或少地遵循簡單博客示例的教程,我的列表/編輯/新控制器看起來像它(有限制和搜索)https://github.com/maddy2101/simpleblog/blob/master/Classes/Controller/BlogController .php。所有的工作。關於setVariablesToRender()我只是遵循其他例子,如https://www.snip2code.com/Snippet/282519/TYPO3--Extbase--Simple-JsonView-usage。 –

+0

我玩了一會兒,得到以下錯誤:無法找到名稱「Vendor \ Lei \ Domain \ Model \ Json」的類定義。這可能是由類定義中的類名錯誤拼寫引起的。 –

回答

2

extbase exxts域名對象稱爲Json。如果您只想渲染現有的域對象作爲其JSON表示,只需使用您在listAction()detailAction()中使用的相同存儲庫。

看一看我的例子:https://usetypo3.com/json-view.html

此外,return就像你在你的倉庫後做一個調試將永遠不會被執行。

+0

啊......謝謝你的提示。現在使用相同的存儲庫,它正在工作!我上面更新了!非常感謝! –