【问题标题】:Cakephp 3.0.3 How do I use the default layout?Cakephp 3.0.3 如何使用默认布局?
【发布时间】:2015-07-24 20:57:02
【问题描述】:

我刚刚用 composer 安装了 cakephp 3.0.3,我正在运行内置的开发蛋糕服务器来查看我的网站。我只对项目进行了两次更改。使用该项目一段时间后,我意识到我的页面上没有加载 default.ctp 布局。

我做的第一件事是配置与 MySQL 的数据库连接。接下来我想做的是包括 bootstrap 和 jQuery。自然,我按照 cakephp 食谱将 css 和 js 放在 webroot 下的相应文件夹中。接下来我链接到 src/Templates/Layout/default.ctp 中的那些文件。

一开始没有出现,我猜应该是链接它们的命令有问题,但是当我尝试在default.ctp中添加其他要显示的内容时,我意识到布局没有呈现完全没有。

我对 cakephp 和 php 完全陌生,但我直接从食谱中阅读并按照他们的示例进行操作,我不知道为什么根本不会加载默认布局。

我尝试将 appController 更改为包含 $this->layout = 'default'; 作为建议 here 但这只是给了我一个严格的错误,说 appController beforeRender() 必须与 Controller beforeRender() 兼容所以我只是将其改回原样默认情况下。

我正在使用 OS X Yosemite 和 Google Chrome 来查看页面,如果这有影响的话。默认主页上的所有内容都有复选标记,表示所有内容都已正确安装和配置,我看到其他人有类似问题,mod_rewrite 是他们的问题,但我没有关于 mod_rewrite 的任何错误。

这是我的默认.ctp:

<?php
/**
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @since         0.10.0
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */



$cakeDescription = "Children's Medical Group";
?>

<!-- This layout is loaded for controller view content -->


<!DOCTYPE html>
<html>
<head>
    <?= $this->Html->charset() ?>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>
        <?= $cakeDescription ?>:
        <?= $this->fetch('title') ?>
    </title>
    <?= $this->Html->meta('icon') ?>

    <?= $this->Html->css('base.css') ?>
    <?= $this->Html->css('cake.css') ?>

    <?= $this->fetch('meta') ?>

    <!-- The following css and js elements are for bootstrap -->

        <!-- Latest compiled and minified CSS -->
    <!-- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> -->
    <?= $this->Html->css('bootstrap.min.css') ?>

        <!-- Optional theme -->
    <!--<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"> -->
    <?= $this->Html->css('bootstrap-theme.min.css') ?>

    <!-- Not actually sure what this does -->
    <?= $this->fetch('css') ?>


</head>
<body>
    <header>
        <div class="header-title">
            <span><?= $this->fetch('title') ?></span>
        </div>
        <div class="header-help">
            <span><a target="_blank" href="http://book.cakephp.org/3.0/">Documentation</a></span>
            <span><a target="_blank" href="http://api.cakephp.org/3.0/">API</a></span>
        </div>
    </header>

    <div id='.jumbotron'>
    Testing both bootstrap and default.ctp to see if this shows
    </div>

    <div id="container">

        <div id="content">
            <?= $this->Flash->render() ?>

            <div class="row">
                <?= $this->fetch('content') ?>
            </div>
        </div>
        <footer>
        </footer>
    </div>

           <!-- Latest compiled and minified Bootstrap JavaScript -->
    <!-- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> -->
    <?= $this->Html->script('bootstrap.min.js'); ?>

    <!-- Bootstrap requires jQuery to run bootstrap.js-->
    <?= $this->Html->script('jquery-1.11.3.min.js'); ?>

    <!-- Not actually sure what this does -->
    <?= $this->fetch('script') ?>
</body>
</html>

还有我访问http://localhost:8765/时看到的html源码:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>
        CakePHP: the rapid development php framework    </title>
    <link href="/favicon.ico" type="image/x-icon" rel="icon"/><link href="/favicon.ico" type="image/x-icon" rel="shortcut icon"/>    <link rel="stylesheet" href="/css/base.css"/>    <link rel="stylesheet" href="/css/cake.css"/></head>
<body class="home">
    <header>
        <div class="header-image">
            <img src="http://cakephp.org/img/cake-logo.png" alt=""/>            <h1>Get the Ovens Ready</h1>
        </div>
    </header>
    <div id="content">
                <p id="url-rewriting-warning" style="background-color:#e32; color:#fff;display:none">
            URL rewriting is not properly configured on your server.
            1) <a target="_blank" href="http://book.cakephp.org/3.0/en/installation/url-rewriting.html" style="color:#fff;">Help me configure it</a>
            2) <a target="_blank" href="http://book.cakephp.org/3.0/en/development/configuration.html#general-configuration" style="color:#fff;">I don't / can't use URL rewriting</a>
        </p>

        <div class="row">
            <div class="columns large-5 platform checks">
                                    <p class="success">Your version of PHP is 5.4.16 or higher.</p>

                                    <p class="success">Your version of PHP has the mbstring extension loaded.</p>

                                    <p class="success">Your version of PHP has the openssl extension loaded.</p>

                                    <p class="success">Your version of PHP has the intl extension loaded.</p>
                            </div>
            <div class="columns large-6 filesystem checks">
                                    <p class="success">Your tmp directory is writable.</p>

                                    <p class="success">Your logs directory is writable.</p>

                                                    <p class="success">The <em>FileEngine</em> is being used for core caching. To change the config edit config/app.php</p>
                            </div>
        </div>
        <div class="row">
            <div class="columns large-12  database checks">
                                                    <p class="success">CakePHP is able to connect to the database.</p>
                            </div>
        </div>
        <div class="row">
            <div class="columns large-6">
                <h3>Editing this Page</h3>
                <ul>
                    <li>To change the content of this page, edit: src/Template/Pages/home.ctp.</li>
                    <li>You can also add some CSS styles for your pages at: webroot/css/.</li>
                </ul>
            </div>
            <div class="columns large-6">
                <h3>Getting Started</h3>
                <ul>
                    <li><a target="_blank" href="http://book.cakephp.org/3.0/en/">CakePHP 3.0 Docs</a></li>
                    <li><a target="_blank" href="http://book.cakephp.org/3.0/en/tutorials-and-examples/bookmarks/intro.html">The 15 min Bookmarker Tutorial</a></li>
                    <li><a target="_blank" href="http://book.cakephp.org/3.0/en/tutorials-and-examples/blog/blog.html">The 15 min Blog Tutorial</a></li>
                </ul>
                <p>
            </div>
        </div>

        <hr/>
        <div class="row">
            <div class="columns large-12">
                <h3 class="">More about Cake</h3>
                <p>
                    CakePHP is a rapid development framework for PHP which uses commonly known design patterns like Front Controller and MVC.
                </p>
                <p>
                    Our primary goal is to provide a structured framework that enables PHP users at all levels to rapidly develop robust web applications, without any loss to flexibility.
                </p>

                <ul>
                    <li><a href="http://cakefoundation.org/">Cake Software Foundation</a>
                    <ul><li>Promoting development related to CakePHP</li></ul></li>
                    <li><a href="http://www.cakephp.org">CakePHP</a>
                    <ul><li>The Rapid Development Framework</li></ul></li>
                    <li><a href="http://book.cakephp.org/3.0/en/">CakePHP Documentation</a>
                    <ul><li>Your Rapid Development Cookbook</li></ul></li>
                    <li><a href="http://api.cakephp.org/3.0/">CakePHP API</a>
                    <ul><li>Quick Reference</li></ul></li>
                    <li><a href="http://bakery.cakephp.org">The Bakery</a>
                    <ul><li>Everything CakePHP</li></ul></li>
                    <li><a href="http://plugins.cakephp.org">CakePHP plugins repo</a>
                    <ul><li>A comprehensive list of all CakePHP plugins created by the community</li></ul></li>
                    <li><a href="https://groups.google.com/group/cake-php">CakePHP Google Group</a>
                    <ul><li>Community mailing list</li></ul></li>
                    <li><a href="irc://irc.freenode.net/cakephp">irc.freenode.net #cakephp</a>
                    <ul><li>Live chat about CakePHP</li></ul></li>
                    <li><a href="https://github.com/cakephp/">CakePHP Code</a>
                    <ul><li>For the Development of CakePHP Git repository, Downloads</li></ul></li>
                    <li><a href="https://github.com/cakephp/cakephp/issues">CakePHP Issues</a>
                    <ul><li>CakePHP issues and pull requests</li></ul></li>
                </ul>
            </div>
        </div>
    </div>
    <footer>
    </footer>
<script>var __debug_kit_id = 'cade8b34-e4ea-4601-a30e-a3a941ddf855', __debug_kit_base_url = 'http://localhost:8765/';</script><script src="/debug_kit/js/toolbar.js"></script></body>
</html>

【问题讨论】:

    标签: php css cakephp cakephp-3.0


    【解决方案1】:
    1. 您已经安装了 3.0.3 版的 CakePHP 应用程序模板,实际的 CakePHP 框架 有所不同(请参阅 bin/cake --helpvendor/cakephp/cakephp/VERSION.txt) .建议您同时提及这两个版本!

    2. 除非您知道自己在做什么,即您知道如何迁移代码,否则不要使用为旧版本制作的问题/答案、教程等。您链接到的问题是关于 CakePHP 1.3,它与它一样不兼容。

    3. 您正在查看默认主页(通过现成的pages controller 提供),它在模板文件本身中使用$this-&gt;layout = false; 明确地turns off the layout usage (src/Template/Pages/home.ctp)。

      李>

    因此,创建您自己的页面,除非您明确禁用该行为,否则它们将使用默认布局。如果要更改主页,请按照页面本身的说明进行操作

    要更改此页面的内容,请编辑:src/Template/Pages/home.ctp

    【讨论】:

      【解决方案2】:
      $this->viewBuilder()->layout('default');
      

      【讨论】:

      • 与半年前的《狂人答案》的相似度惊人。
      • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。也请尽量不要用解释性的 cmets 挤满你的代码,这会降低代码和解释的可读性!
      【解决方案3】:

      您可以使用以下内容:- $this->viewBuilder()->layout('Layout_Name');

      我不确定,但它应该可以工作......

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多