【发布时间】:2018-09-21 20:03:12
【问题描述】:
在我的 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));
}
我收到此错误:
错误:找不到 PhotoalbumsController::imgToAlbum() 的视图。
确认您已创建文件: 以下路径之一中的“Admin/Photoalbums/img_to_album.ctp”
我也试过$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');
}
}
我在这里错过了什么。
【问题讨论】:
-
也许在动作之后和“渲染前”阶段之前运行的代码会改变布局/模板,例如请求处理程序组件!?
-
错误很明显。您是否创建了文件:“Admin/Photoalbums/img_to_album.ctp”?
-
@danny3b,你是对的,错误很明显。但我应该能够在
src/Template/Layout中加载 ajax.ctp 文件。虽然我也想在其他控制器上使用这个文件。为每个控制器创建一个单独的文件是很疯狂的。 -
@ndm,有没有办法检查?
-
我不知道你到底想达到什么目标。您是否只需要用于 ajax 调用的“ajax”模板?在那种情况下,视图根本不应该是渲染器,通过设置
$this->autoRender = false;
标签: php cakephp cakephp-3.5