Php Extbase无法识别自定义JsonView
我使用的是typo37.6。 我无法让extbase接受我的自定义JsonView,它似乎无法识别它 我覆盖默认的JsonView,如下所示:Php Extbase无法识别自定义JsonView,php,json,extbase,typo3-7.6.x,Php,Json,Extbase,Typo3 7.6.x,我使用的是typo37.6。 我无法让extbase接受我的自定义JsonView,它似乎无法识别它 我覆盖默认的JsonView,如下所示: use TYPO3\CMS\Extbase\Mvc\View\JsonView as ExtbaseJsonView; class JsonView extends ExtbaseJsonView { /** * @var array */ protected $configuration = [ 'jobs' => [
use TYPO3\CMS\Extbase\Mvc\View\JsonView as ExtbaseJsonView;
class JsonView extends ExtbaseJsonView
{
/**
* @var array
*/
protected $configuration = [
'jobs' => [
'_exclude' => ['pid'],
'_descend' => [
'place' => [
'_only' => ['name']
]
]
],
];
}
'somearrayvalue' => array(
* '_descendAll' => array(
* '_only' => array('property1')
* )
* )
编辑:根据这个
现在我不明白为什么我仍然以Json的形式得到这个输出:
[{"description":"Owns products","pensum":100,"pid":55,"test":"Product","title":"Product Owner","uid":1}]
其中一个pid仍然存在,即使它位于排除字段中,并且该位置未被输出。extbase似乎忽略了我的覆盖,但我不知道为什么,也没有抛出错误。我将自定义JsonView放入class/View/JsonView.php中
我的模型是:
工作
地点
/**
* Places
*/
class Place extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
/**
* name
*
* @var string
* @validate NotEmpty
*/
protected $name = '';
/**
* numberOfEmployees
*
* @var int
* @validate NotEmpty
*/
protected $numberOfEmployees = 0;
/**
* acquired
*
* @var \DateTime
* @validate NotEmpty
*/
protected $acquired = null;
// below getter and setter
}
控制器
/**
* JobAjaxController
*/
class JobAjaxController extends ActionController
{
/**
* @var string
*/
protected $defaultViewObjectName = \TYPO3\CMS\Extbase\Mvc\View\JsonView::class;
/**
* jobRepository
*
* @var \Vendor\SfpJobs\Domain\Repository\JobRepository
* @inject
*/
protected $jobRepository = NULL;
/**
* placeRepository
*
* @var \Vendor\SfpJobs\Domain\Repository\PlaceRepository
* @inject
*/
protected $placeRepository = NULL;
/**
* action list
* This function
*
* @param \Vendor\SfpJobs\Domain\Model\Job $job
* @return void
*/
public function listAction()
{
$jobs = $this->jobRepository->findAll();
$this->view->assign('jobs', $jobs);
$this->view->setVariablesToRender(array('jobs'));
}
}
我找到了答案,有两个错误,一个是我的错,另一个在我看来像是糟糕的文档。 我仍然在控制器中使用默认的Jsonview 所以我需要
protected $defaultViewObjectName = \Vendor\Jobs\View\JsonView::class;
而不是
protected $defaultViewObjectName = \TYPO3\CMS\Extbase\Mvc\View\JsonView::class;
第二个是我错误的Jsonview配置。
上面写着
我认为variable3可以是一个数组,但事实并非如此。数组的符号如下所示:
use TYPO3\CMS\Extbase\Mvc\View\JsonView as ExtbaseJsonView;
class JsonView extends ExtbaseJsonView
{
/**
* @var array
*/
protected $configuration = [
'jobs' => [
'_exclude' => ['pid'],
'_descend' => [
'place' => [
'_only' => ['name']
]
]
],
];
}
'somearrayvalue' => array(
* '_descendAll' => array(
* '_only' => array('property1')
* )
* )
所以最后我有了这个配置
protected $configuration = [
'jobs' => [
'_descendAll' => [
'_exclude' => ['pid'],
'_descend' => [
'place' => [
'_only' => ['name']
]
]
]
],
];