在yii创建应用成果之后,在view/layouts/目录下,会产生3个布局页面:
-->main.php
-->column1.php
-->column2.php
由于首次使用的是命令行Shell方式创建的应用,yii本身会创建一个控制器组件:Controller.php,它继承了CController控制器,该文件位于/components目录下:
+++++++++++++++++++++++++++++++++++++++++++
<?php
class Controller extends CController
{
public $layout=‘//layouts/column1‘;
public $menu=array();
public $breadcrumbs=array();
}
?>
+++++++++++++++++++++++++++++++++++++++++++
文件里设置了layout的默认页面为: ‘//layouts/column1′,然后再view/layouts/中,column1再次调用main.php视图文件:
+++++++++++++++++++++++++++++++++++++++++++
<?php $this->beginContent(‘//layouts/main‘); ?>
<div id="content">
<?php echo $content; ?>
</div><!-- content -->
<?php $this->endContent(); ?>
+++++++++++++++++++++++++++++++++++++++++++
加载完main.php文件之后,在包含index.php中的内容即$content中的内容.
如果控制器都是由Gii这个脚手架自动生成,那么所有的控制器都会继承都是继承于Controller而非官方所说的继承与CController控制器,在页面视图渲染,多了一层column1.php中间视图.
所以说yii在 $this->render(‘index’) 一个页面的时候,使用 column1.php 包含 main.php,再由 main.php 包含 index.php,最后返回内容.(这是针对于继承Controller方式)。
而至于column2.php只干什么的呢,貌似是个打酱油的,没有用到。
如果我们想更改默认的layout视图文件,要么直接在Components/Controller.php更改$layout = ‘//layouts/newlayout_name‘,要么控制器继承时,直接 extends CController 而不是Controller,然后配置config/main.php:
++++++++++++++++++++++++++++++++++++++++++++
return array(
………………
‘basePath‘=>dirname(__FILE__).DIRECTORY_SEPARATOR.‘..‘,
‘name‘=>‘web application‘,
‘layout‘=>‘newlayout_name‘
………………
)
+++++++++++++++++++++++++++++++++++++++++++++
然后在控制器里调用:
+++++++++++++++++++++++++++++++++++++++++++++
//TestController为自定义一个测试控制器,直接继承CController,默认是继承controller
class TestController extends CController{
………………
}
++++++++++++++++++++++++++++++++++++++++++++++
当然也可以直接在控制器里设置layout属性,覆盖默认的layout,使得视图渲染更灵活.此处只是为说明 /view 下layouts/中,main.php和column1.php,以及index.php之间的关系.
++++++++++++++++++++++++++++++++++++++++++++++
//直接在controller中如下覆盖设置layout
public $layout=‘//layouts/newlayout_name‘;
++++++++++++++++++++++++++++++++++++++++++++++