YII前臺和後臺佈局(轉載)

1、gii自動生成佈局模塊
1> 在config中main.php中開啓gii,即:把下面的註釋去掉
'modules'=>array(
        // uncomment the following to enable the Gii tool
        /*
        'gii'=>array(
            'class'=>'system.gii.GiiModule',
            'password'=>'Enter Your Password Here',  //此處設置2的gii密碼
            // If removed, Gii defaults to localhost only. Edit carefully to taste.
            'ipFilters'=>array('127.0.0.1','::1'),
        ),
        */
    ),
2> 輸入gii地址(如:shop項目,http://localhost/shop/index.php?r=gii/default/login),進入gii密碼界面;
a,輸入密碼(上面「1>」設置好的);
b,進入頁面,選擇最後一個佈局建立(Module Generator);
c,而後在Module ID的輸入框中,輸入你的模塊名稱,如:houtai 再點擊Preview 繼續點擊Generate;再回protected查看是否存在modules/houtai文件,存在就OK完成自動建立gii的houtai模塊了。
3> 開啓模塊調用功能,把houtai加入main.php的modules中,即,在modules中加入"houtai"元素值;以下:
'modules'=>array(
        // uncomment the following to enable the Gii tool        
        'gii'=>array(
            'class'=>'system.gii.GiiModule',
            'password'=>'123321',
            // If removed, Gii defaults to localhost only. Edit carefully to taste.
            'ipFilters'=>array('127.0.0.1','::1'),
        ),
                'houtai',
    ),
2、後臺layout文件的調用與設置
1> 因爲shop項目(下方統稱爲前端頁),也有一個對應的layout;即:你的實際盤根路徑\shop\protected\components\Controller.php和你的實際盤根路徑\shop\protected\views\layouts\column1.php兩個調用和設置文件了。在默認狀態下,gii建立的houtai(下方統稱後臺)若是用render開啓佈局渲染的話,會直接把前端頁的父類佈局內容調出來。固然這不是咱們要的結果!

因此,咱們再設置一下,讓後臺調用新的佈局。
設置方法:
1)直接複製前端佈局的相應2個文件,粘進houtai對應文件夾,文件夾名確定要本身新增的,效果以下:
a> houtai\components\Controller.php
b> houtai\views\layouts\column1.php(column1.php可任意換名,如xx.php)

2)修改houtai\components\Controller.php中,把
public $layout='//layouts/column1';中開頭的雙斜扛,去掉一個(由於://,則默認會加載protected/view/layouts/column1.php這個layout;),column1改爲你本身新名字(xx.php);效果以下:
public $layout='/layouts/xx';
3、column1.php設置
跟模板的替換原則同樣,用$content作變量,加載頁面時,換成render渲染的內容。
注:我的建議,不要把局部重用的頁面內容(如:產品左側類別塊)也想丟進這個頁面,你能夠直接require_once包含進去,效果相差不大的。php

相關文章
相關標籤/搜索