如何在cakephp 3.x中创建模板

时间:2017-08-14 03:43:13

标签: cakephp-3.x

我是CakePHP 3的新生,请解决我的问题。 这是我的控制器文件:

DirectUseController.php

<?php
class DirectUseController extends AppController {
    function index() {
        $this->layout = 'directuse';
    }
}
?>

这是我的布局文件:

directuse.ctp

<!DOCTYPE html>
<html>
    <head>
        <title>
            <?= $this->fetch('title') ?>
        </title>
        </head>
    <body>
    <a href="#"> Bootstrap </a> | <a href="#"> Foundation </a> | <a href="#"> Materilize </a> 
    <br><br>
    Copyright
    <br><br>
  </body>
</html>

这是我直接使用的文件夹中的索引文件 的 index.ctp

<section id="mainBody">
      hello 
</section>

我的文件夹结构是:
enter image description here

我错过了什么?

3 个答案:

答案 0 :(得分:2)

你的布局应该包含在某个地方:

echo $this->fetch('content');

如果这不能解决您的问题,那么您将需要更具体地解决问题所在。

答案 1 :(得分:0)

  • CakePHP优先考虑convention配置,因此请尝试将您的控制器名称更改为直接 se s 控制器,同时更改布局文件夹的名称(对于DirectUses)也许你的模型(蛋糕烘焙可以很容易地帮助你),我不知道你的[数据库中的表名]但它应该是复数和小写(directuses)(如果你不使用数据库)那是另一个故事)

  • 对于您的模板,Greg Schmidt是right

答案 2 :(得分:0)

在布局文件中,你必须使用它:

<?= $this->Flash->render(); ?>
<?= $this->fetch('content'); ?>

我更喜欢在<html><body>布局中添加default.ctpdirectuse.ctp标记。这样你就不必每次都重建你的html。这将使您的代码更清洁。

相关问题