Yii中默认布局的修改方法

Server 林涛 14874℃ 0评论

用Yii创建web应用之后,默认会在view/layouts/目录下产生3个布局页面:

main.php
column1.php
column2.php

使用命令行Shell方式创建的应用,yii本身会创建一个控制器组件:Controller.php 继承了CController控制器,该文件位于/components目录下。

</pre>
//componets/Controller.php文件内容如下

/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController
{
 /**
 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
 */
 public $layout='//layouts/column1';
 ... ...
}
<pre>
文件里设置了layout的默认页面为: ‘//layouts/column1′,然后再view/layouts/中,column1再次调用main.php视图文件,包裹了两个div层.
</pre>
//column1.php文件内容

<?php $this->beginContent('//layouts/main'); ?>
<div class="container">
 <div id="content">
 <?php echo $content; ?>
 <!-- content -->

<?php $this->endContent(); ?>
<pre>

包裹的两个div层,默认class名称为container,这可能与自己命名的container有冲突,需要视实际情况做一些调整,加载完main.php文件之后,在包含index.php中的内容即$content中的内容.

如果控制器都是由Gii这个脚手架自动生成,那么所有的控制器都会继承都是继承于Controller而非官方所说的继承与CController控制器,在页面视图渲染,多了一层column1.php中间视图.

所以说yii在 $this->render(‘index’) 一个页面的时候,使用 column1.php 包含 main.php , 再由 main.php 包含 index.php,最后返回内容.(这是针对于继承Controller方式).

如果我们想更改默认的layout视图文件,要么直接在Components/Controller.php更改$layout = ‘//layouts/newlayout_name’,要么控制器继承时,直接 extends CController 而不是Controller,然后配置config/main.php:

</pre>
//更改config/main.php,添加layout应用级别layout
return array(
 ... ...
 'name' => 'Web Application',
 'layout' => 'newlayout_name',
 ... ...
)
<pre>

然后控制器中调用:


</pre>
//TestController 自定义一个测试控制器
class TestController extends CController{
 ... ...
}
<pre>

内容参考自:http://blog.webnotes.cn/the-relationship-between-main-php-and-column-php-in-layout-on-yii/

如需转载请注明: 转载自26点的博客

本文链接地址: Yii中默认布局的修改方法

转载请注明:26点的博客 » Yii中默认布局的修改方法

喜欢 (0)
发表我的评论
取消评论

表情