【问题标题】:Cakephp 3 Jsonview and AdminLTECakephp 3 Jsonview 和 AdminLTE
【发布时间】:2018-10-24 20:37:33
【问题描述】:

我正在使用当前的 Cakephp 3.6.3 并通过 composer 附加

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');

没有任何成功。

【问题讨论】:

    标签: json ajax request cakephp-3.x adminlte


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-08
      • 2019-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多