Cakephp 3.0.3 How to use the default layout?

I just installed cakephp 3.0.3 with composer and I am running the built-in development cake server to view my site. I only made two changes to the project. After working with the project for a bit, I realized that the default.ctp layout is not loading on my pages.

The first thing I did was set up a connection to the MySQL database. The next thing I wanted to do was include bootstrap and jQuery. Naturally I followed the cakephp cookbook and put the css and js in their respective folders under the webroot. Then I linked to these files at src / Templates / Layout / default.ctp.

At first, when they weren't showing, I assumed it was a problem with the command linking them, but when I tried to add other content to display in default.ctp I realized that the layout is not rendering at all.

I'm completely new to cakephp and php, but I was reading straight from the cookbook and just following their example, I don't know why the default layout won't load at all.

I tried changing the appController to include $this->layout = 'default';

as suggested here , but that just gave me a severe error saying that the beforeRender () appController should be compatible with the beforeRender () controller, so I just changed it to be the default.

I am using OS X Yosemite and Google Chrome to view the page if that matters. The default home page has marks that everything is installed and configured correctly, and I've seen other people have similar problems where mod_rewrite is their problem, but I don't have any bugs in mod_rewrite.

Here is my default.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 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>

      

and the html source I see when I visit http: // localhost: 8765 / :

<!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>

      

+3


source to share


3 answers


  • You have installed version 3.0.3 of the CakePHP Application Template, the actual framework of CakePHP is something else (see bin/cake --help

    or vendor/cakephp/cakephp/VERSION.txt

    ). It is recommended to specify both versions!

  • Don't use questions / answers, tutorials, etc. made for older versions unless you know what you are doing, i.e. you know how to port your code. The question you linked is about CakePHP 1.3, which is incompatible with what it gets.

  • You are viewing the default homepage (supplied via the out of the box page server ), it explicitly disables the use of layout using $this->layout = false;

    in the template file itself ( src/Template/Pages/home.ctp

    ).

So, create your own pages, they will use the default layout unless you explicitly disable this behavior. And if you want to change the home page, do as the page itself says



To change the content of this page, edit: src / Template / Pages / home.ctp

+5


source


$this->viewBuilder()->layout('default');

      



+3


source


you can use the following: - $ This-> viewBuilder () -> layout ('layout_name');

I'm not sure, but it should work ...

0


source







All Articles