Cakephp 3.0.3 How do I use the default layout?

16,367

Solution 1

  1. You have installed version 3.0.3 of the CakePHP Application template, the actual CakePHP framework is something different (see bin/cake --help or vendor/cakephp/cakephp/VERSION.txt). It is advised that you mention both versions!

  2. Do not use questions/answers, tutorials, etc made for older versions unless you know what you are doing, ie you know how to migrate the code. The question you've linked to is about CakePHP 1.3, which is as incompatible as it gets.

  3. You are viewing the default home page (delivered via the ready made pages controller), it explicitly turns off the layout usage 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 that 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

Solution 2

$this->viewBuilder()->layout('default');
Share:
16,367
Matt
Author by

Matt

Updated on June 04, 2022

Comments

  • Matt
    Matt almost 2 years

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

    The first thing I did was configure the database connection to MySQL. 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 appropriate folders under webroot. Next I linked to those files in src/Templates/Layout/default.ctp.

    At first when they did not show up, I guessed it must have been an issue with the command linking them, but when I tried to add other content to be displayed within default.ctp, I realized that the layout is not rendering at all.

    I am totally new to cakephp and php, but I read straight from the cookbook and just followed their example, I don't know why the default layout would not be loaded at all.

    I tried to change the appController to include $this->layout = 'default'; as suggested here but that just gave me a strict error saying that appController beforeRender() must be compatible with Controller beforeRender() so I just changed it back to the way it was by default.

    I am using OS X Yosemite and Google Chrome to view the page if that makes a difference. Everything on the default homepage has check marks saying that everything is installed and configured properly, and I have seen other people that have similar issues where mod_rewrite is their problem but I do not have any errors about 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'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>
    

    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>