我正在使用当前的Cakephp 3.6.3并通过作曲家附加
composer require maiconpinto/cakephp-adminlte-theme
在Maicon Pinto的教程中,介绍了如何设置控制器和视图。到目前为止一切都很好。 现在我正试图通过我网站上的ajax使用json。 route.php中的所有扩展都按照中所述完成 https://book.cakephp.org/3.0/en/views/json-and-xml-views.html
在控制器中我正在使用
$this->loadComponent('RequestHandler');
包括在内。
我现在遇到的问题是,总是抛出MissingTemplateException:
Error: The view for NameController::action() was not found.
这是调试网站输出的一部分:
response => object(Cake\Http\Response) {
'status' => (int) 200,
'contentType' => 'application/json',
'headers' => [
[maximum depth reached]
],
'file' => null,
'fileRange' => [[maximum depth reached]],
'cookies' => object(Cake\Http\Cookie\CookieCollection) {},
'cacheDirectives' => [[maximum depth reached]],
'body' => ''
}
paginate => []
components => []
View => object(AdminLTE\View\AdminLTEView) {
Blocks => object(Cake\View\ViewBlock) {}
plugin => null
name => '<Modelname>'
passedArgs => [
[maximum depth reached]
]
helpers => [[maximum depth reached]]
templatePath => '<Modelname>'
template => 'fill'
layout => 'default'
layoutPath => null
autoLayout => true
subDir => null
theme => 'AdminLTE'
hasRendered => false
uuids => [[maximum depth reached]]
request => object(Cake\Http\ServerRequest) {}
response => object(Cake\Http\Response) {}
elementCache => 'default'
viewClass => null
viewVars => [
[maximum depth reached]
]
Form => object(AdminLTE\View\Helper\FormHelper) {}
[protected] _helpers => object(Cake\View\HelperRegistry) {}
[protected] _ext => '.ctp'
[protected] _passedVars => [
[maximum depth reached]
]
[protected] _paths => [
[maximum depth reached]
]
[protected] _pathsForPlugin => [[maximum depth reached]]
[protected] _parents => [[maximum depth reached]]
[protected] _current => null
[protected] _currentType => ''
[protected] _stack => [[maximum depth reached]]
[protected] _viewBlockClass => 'Cake\View\ViewBlock'
[protected] _eventManager => object(Cake\Event\EventManager) {}
[protected] _eventClass => 'Cake\Event\Event'
[protected] _viewBuilder => null
}
那么缺少什么,Cakephp没有请求模板,而是使用文档中预见的普通JsonView?它看起来,因为框架已正确检测到JSON数据并且还希望发送JSON响应。我还尝试将我的动作中的viewBuilder设置为
$this->viewBuilder('Json');
没有任何成功。
答案 0 :(得分:0)
我通过更改beforeRender中的类名来找到解决方案:
public function beforeRender(\Cake\Event\Event $event) {
parent::beforeRender($event);
if ($this->getRequest()->is('ajax') || $this->getRequest()->is('json')){
$this->viewBuilder()->setClassName('Json');
}
}
现在我的格式正确。