CakePHP3.5加载不同的模板文件

CakePHP3.5 load different template file

在我的 PhotoalbumsController 中,我试图在调用操作 imgToAlbum 时加载不同的布局。

<?php
namespace App\Controller\Admin;

use App\Controller\AppController;
use Cake\ORM\TableRegistry;
use Cake\Http\ServerRequest;
use Cake\Event\Event;

class PhotoalbumsController extends AppController 
{
    public function initialize()
    {
        parent::initialize();
    }

    ...

    public function imgToAlbum()
    {
        ...        
        $this->viewBuilder()->setLayout('ajax');
        $content = 'test';
        $this->set(compact($content));
    }

我收到此错误:

Error: The view for PhotoalbumsController::imgToAlbum() was not found.

Confirm you have created the file: "Admin/Photoalbums/img_to_album.ctp" in one of the following paths

我也试过了$this->viewBuilder()->setTemplate('ajax');$this->viewBuilder()->template('ajax');。但是这些也不管用。

我在我的后端 AppController 中使用相同的技巧,即,这有效:

public function beforeRender(Event $event) 
{
    parent::beforeRender($event);
    if($this->request->getParam('prefix') and $this->request->getParam('prefix') == 'admin') {
        $this->viewBuilder()->setLayout('admin');
    }
}

我在这里错过了什么。

这应该可以解决问题:

$this->autoRender = false;