Cakephp 如何覆盖正在查看的页面

Cakephp 如何覆盖正在查看的页面,cakephp,Cakephp,我将pages_controller.php复制到我的app/controllers文件夹中,然后创建了一个视图并将其放置在pages下。我在视图下有一个自定义的_layout.ctp。我遇到的问题是,我放在页面下的视图显示为默认的cakephp外观,但我想使用我的自定义_布局。我尝试在代码中添加最后一行,但没有 <?php /** * Static content controller. * * This file will render views from views/page

我将pages_controller.php复制到我的app/controllers文件夹中,然后创建了一个视图并将其放置在pages下。我在视图下有一个自定义的_layout.ctp。我遇到的问题是,我放在页面下的视图显示为默认的cakephp外观,但我想使用我的自定义_布局。我尝试在代码中添加最后一行,但没有

<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       cake
 * @subpackage    cake.cake.libs.controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       cake
 * @subpackage    cake.cake.libs.controller
 * @link http://book.cakephp.org/view/958/The-Pages-Controller
 */
class PagesController extends AppController {

/**
 * Controller name
 *
 * @var string
 * @access public
 */
    var $name = 'Pages';

/**
 * Default helper
 *
 * @var array
 * @access public
 */
    var $helpers = array('Html', 'Session');

/**
 * This controller does not use a model
 *
 * @var array
 * @access public
 */
    var $uses = array();

/**
 * Displays a view
 *
 * @param mixed What page to display
 * @access public
 */
    function display() {
        $path = func_get_args();

        $count = count($path);
        if (!$count) {
            $this->redirect('/');
        }
        $page = $subpage = $title_for_layout = null;

        if (!empty($path[0])) {
            $page = $path[0];
        }
        if (!empty($path[1])) {
            $subpage = $path[1];
        }
        if (!empty($path[$count - 1])) {
            $title_for_layout = Inflector::humanize($path[$count - 1]);
        }
        $this->set(compact('page', 'subpage', 'title_for_layout'));
        $this->render(implode('/', $path));

        $this->layout = 'custom_layout';

    }
}

//在控制器中添加:
$this->layout=“自定义布局”;

您需要在调用
render
之前更改布局,因为
render
使用当前布局渲染视图

//in your controller add: $this->layout = "custom_layout";