【问题标题】:CakePHP 3.x - viewBuilding()->setLayout('blank') not workingCakePHP 3.x - viewBuilding()->setLayout('blank') 不工作
【发布时间】:2020-02-21 22:44:14
【问题描述】:

我正在尝试将布局设置为 blank.ctp 但它不起作用,我在 src/Template/Layout 中创建了一个 blank.ctp 但以下代码不起作用,因为在 blank.ctp 文件中没有显示时,默认布局是。

public function delete($id = null)
{
    $this->request->allowMethod(['get']);
    $this->loadModel('AdminLTETasks');
    $adminLTETask = $this->AdminLTETasks->get($id);
    if ($this->AdminLTETasks->delete($adminLTETask)) {
        echo 'The admin l t e task has been deleted.';
    } else {
        echo 'The admin l t e task could not be deleted. Please, try again.';
    }

    $this->viewBuilder()->setLayout('blank');
}

我也尝试过不推荐使用的方法 layout('blank') 并且它也不起作用

public function delete($id = null)
{
    $this->request->allowMethod(['get']);
    $this->loadModel('AdminLTETasks');
    $adminLTETask = $this->AdminLTETasks->get($id);
    if($adminLTETask->user_id == $this->Auth->user('id')) {
        if ($this->AdminLTETasks->delete($adminLTETask)) {
            echo 'The admin l t e task has been deleted.';
        } else {
            echo 'The admin l t e task could not be deleted. Please, try again.';
        }
    }

    $this->viewBuilder()->layout('blank');
}

【问题讨论】:

  • 怎么不工作了?有错误吗?是否正在加载空白.ctp 文件?需要更多详细信息。
  • 正在显示默认布局
  • 布局是否有可能被重置为默认值,例如 beforeRender 回调或 delete.ctp 本身?
  • @GregSchmidt 你一针见血,它在应用程序控制器中被覆盖 beforeRender.... WOOOPS =] 谢谢 Greg

标签: cakephp cakephp-3.x cakephp-3.7


【解决方案1】:

如果您有多种布局,请尝试这种方式。 如果您第一次希望它空白(不是样式......等)

这是您要空白或分配新布局的示例控制器。 类 FrontsController 扩展 AppController { 公共 $title;

public function initialize()
{
    parent::initialize();
    $this->Auth->allow(['index']);
    $this->viewBuilder()->layout('frontend');
}

public function index() {

}

}

还有这个 frontend.ctp 布局示例。

<!DOCTYPE html>
<html lang="en">
 <head>
     <?= $this->element('Fronts/head') ?>
 </head>
 <body>
   <?= $this->element('Fronts/header') ?>
     <!-- Page Content -->
    <div id="content" class="container">
      <?= $this->Flash->render() ?>
      <div class="row">
        <?= ($this->request->getParam('action') == 'index') ? 'LIST' : strtoupper($this->request->getParam('action')); ?>
        <?= $this->fetch('content') ?>
      </div>
    </div>
     <?= $this->element('Fronts/footer') ?>
 </body>
</html>

【讨论】:

  • 您能否解释一下您提供的内容与 OP 的代码之间的关键区别是什么?在我看来,这在功能上与他们已经尝试过的相同。
  • 你好,我发现一些布局(空白)不支持 cakephp 3。所以我也曾经这样做过,但这让我遇到了同样的错误。所以我的解决方案是我为那些我不想要的视图创建一个空布局。我的示例是如何使用多布局模板的示例,就像您在同一个存储库中有一个前端和后端的项目,我们希望为它们分配不同的布局。这是我的解释。感谢您的反馈。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多