如何覆盖正在查看的页面

时间:2011-12-09 04:51:26

标签: cakephp

我将pages_controller.php复制到我的app / controllers文件夹,然后我创建了一个视图并将其放在页面下。我在视图下有一个custom_layout.ctp。我遇到的问题是我放在页面下的视图显示的是cakephp的默认外观,但我想使用我的custom_layout。我尝试在这段代码上添加最后一行,但没有...

<?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';

    }
}

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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