首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cakephp 3 Jsonview和AdminLTE

Cakephp 3 Jsonview和AdminLTE
EN

Stack Overflow用户
提问于 2018-05-15 13:34:26
回答 1查看 449关注 0票数 0

我正在使用当前的Cakephp 3.6.3和via composer additional

代码语言:javascript
复制
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中的说明进行的

在我使用的控制器中

代码语言:javascript
复制
$this->loadComponent('RequestHandler'); 

包括在内。

我现在的问题是,总是会抛出一个MissingTemplateException:

代码语言:javascript
复制
Error: The view for NameController::action() was not found. 

下面是调试站点输出的一部分:

代码语言:javascript
复制
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设置为

代码语言:javascript
复制
$this->viewBuilder('Json');

百无所成。

EN

回答 1

Stack Overflow用户

发布于 2018-05-15 19:45:16

我通过更改beforeRender中的类名找到了解决方案:

代码语言:javascript
复制
public function beforeRender(\Cake\Event\Event $event) {
    parent::beforeRender($event);
    if ($this->getRequest()->is('ajax') || $this->getRequest()->is('json')){
        $this->viewBuilder()->setClassName('Json');
    }
}

现在我得到了正确的格式。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50342950

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档