缺少模板:错误:找不到MyController :: action()的视图。使用自定义View类

时间:2019-05-22 07:24:17

标签: php cakephp cakephp-3.0

使用自定义View类时出错:

要求:我想将我的一些视图登录名分开在不同的View类中,但是我想默认扩展CakePHP提供的AppView.php类。

问题::当我将此视图类添加到控制器动作函数中时,所有ajax调用都进入了src/Template/My/ajax/action.ctp,而当我没有将自定义视图类添加到动作中时,它正在尝试在src/Template/My/action.ctp中找到模板,这是我想要的ctp的路径。

这是我使用的代码:

MyController.php

public function action()
{
    # When I comment below line, everything works properly
    $this->viewBuilder()->setClassName(\App\View\MultiFormatView::class);

    # code ...
}

MultiFormatView.php

<?php

namespace App\View;

use Cake\View\View;

class MultiFormatView extends AppView
{

    /**
     * Returns class for multiple document formats
     *
     * @param  string $filename Filename to get class for.
     * @return string
     */
    public function getFormatIconClass($filename)
    {
        $fileinfo = pathinfo($filename);

        return sprintf('file-%s', $fileinfo['extension']);
    }
}

有什么解决办法吗?

谢谢。

0 个答案:

没有答案
相关问题