Yii 是一個基於組件、純OOP的、用於開發大型 Web 應用的高性能PHP框架。
它將Web編程中的可重用性發揮到極致,能夠顯著加速開發進程 。
Yii適合大流量的應用,如門戶、BBS、CMS及B2B系統等,功能豐富,性能優異
1.在環境變量里添加 E:\yii framework;
2.在環境變量里添加 E:\wamp\bin\php(不添加提示php.exe不是內部命令)或yiic.bat中PHP_COMMAND= E:\wamp\bin\php.exe
3.開啟pdo,pdo_數據庫擴展
為了使用上面提到的 yiic 工具,CLI PHP 程序必須在命令搜索路徑內(譯者注:即php.exe 所在的目錄必須在PATH環境變量中 ),注銷后path配置會生效
1.通過cmd自動化的代碼生成項目骨架
- % cd WebRoot
- % php YiiRoot/framework/yiic webapp "E:\Apache2\htdocs\test"
建立數據庫連接
要在我們創建的程序骨架中使用這個數據庫,我們需要修改它的應用配置 ,它保存在PHP腳本/protected/config/main.php 中。
執行data/schema.sqlite.sql腳本,連接sqlite配置
- 'db'=>array(
- 'connectionString' => 'sqlite:protected/data/blog.db',
- 'tablePrefix' => 'tbl_',
- ),
執行schema.mysql.sql腳本,連接mysql配置
- 'db'=>array(
- 'connectionString' => 'mysql:host=localhost;dbname=blog',
- 'emulatePrepare' => true,
- 'username' => 'root',
- 'password' => '',
- 'charset' => 'utf8',
- 'tablePrefix' => 'tbl_',
- ),
2.創建,讀取,更新,刪除 (CRUD) 是應用的數據對象中的四個基本操作。由於在Web應用的開發中實現CURD的任務非常常見,Yii 為我們提供了一些可以使這些過程自動化的代碼生成工具,名為 Gii
首先我們需要安裝 Gii. 打開文件protected/config/main.php ,添加如下代碼:
- 'import'=>array(
- 'application.models.*',
- 'application.components.*',
- ),
- 'modules'=>array(
- // uncomment the following to enable the Gii tool
- 'gii'=>array(
- 'class'=>'system.gii.GiiModule',
- 'password'=>'Enter Your Password Here',
- //ipFilters用於所在服務器不在本機的情況需開啟
- //'ipFilters'=>array('192.168.1.10','::1'),
- ),
- ),
上面的代碼安裝了一個名為 gii 的模塊,這樣我們就可以通過在瀏覽器中瀏覽如下URL來訪問 Gii 模塊:
http://localhost/test/index.php?r=gii,在彈出的窗口中輸入Enter Your Password Here。如下圖 
創建模型 Model
首先我們需要為每個數據表創建一個模型(Model)類 。模型類會使我們可以通過一種直觀的、面向對象的風格訪問數據庫。稍后我們將會看到這一點。
點擊 Model Generator 鏈接開始使用模型創建工具。
在 Model Generator 頁中,在Table Name一欄輸入 tbl_user (用戶表的名字),
然后按下 Preview 按鈕。一個預覽表將顯示在我們面前。我們可以點擊表格中的鏈接來預覽要生成的代碼。如果一切OK,
我們可以按下 Generate 按鈕來生成代碼並將其保存在一個文件中。
生成的protected/models/User.php包含了繼承自 CActiveRecord 的 User 類,可用於訪問 tbl_user 數據表;
實現CRUD操作
模型類建好之后,我們就可以使用 Crud Generator來創建為這些模型實現CRUD操作的代碼了。我們將對User模型執行此操作。
在 Crud Generator 頁面中,Model Class 一欄輸入User(就是我們剛創建的User模型的名字models/User.php) ,然后按下 Preview 按鈕。
我們會看到有很多文件將被創建。按下Generate按鈕來創建它們。
這樣,我們就使用 yiic工具 生成了對於user表的增刪改查。使用
- http://localhost/test/index.php?r=user
訪問下,試試。。。
protected\components\UserIdentity.php中的登錄密碼
- public function authenticate()
- {
- $users=array(
- // username => password
- 'demo'=>'demo',
- 'admin'=>'admin',
- );
修改成讀取數據表user中的代碼
- <?php
- /**
- * UserIdentity represents the data needed to identity a user.
- * It contains the authentication method that checks if the provided
- * data can identity the user.
- */
- class UserIdentity extends CUserIdentity
- {
- private $_id;
- /**
- * Authenticates a user.
- * @return boolean whether authentication succeeds.
- */
- public function authenticate()
- {
- $user=User::model()->find('LOWER(username)=?',array(strtolower($this->username)));
- if($user===null)
- $this->errorCode=self::ERROR_USERNAME_INVALID;
- else if(!$user->validatePassword($this->password))
- $this->errorCode=self::ERROR_PASSWORD_INVALID;
- else
- {
- $this->_id=$user->id;
- $this->username=$user->username;
- $this->errorCode=self::ERROR_NONE;
- }
- return $this->errorCode==self::ERROR_NONE;
- }
- /**
- * @return integer the ID of the user record
- */
- public function getId()
- {
- return $this->_id;
- }
- }
user.php中添加
- public function validatePassword($password)
- {
- return $password===$this->password;
- }
多模塊(modules)設計
選擇Module Generator-->在Module ID 輸入框中輸入要添加的模塊(例如test)-->點擊Preview按鈕-->點擊Generate按鈕.至此,test模塊便添加完成。對 應目錄為/protected/modules/test目錄
新加模塊訪問地址為:http://website/index.php?r=test
(中級)自定義Gii生成代碼模板
我們使用一個例子來介紹如何定制代碼模板。假設我們想要定制由 model 生成器生成的代碼。
我們首先創建一個名為 protected/gii/model/templates/customer 的目錄。這里的model意味着我們將要 override 默認的 model 生成器。templates/customer意味着我們將增加一個新的代碼模板集名為customer。復制文件 framework/gii/generators/model/templates/default/model.php 到 protected/gii/model/templates/customer。現在是時候做點真正的工作了。打開文件 protected/gii/model/templates/customer/model.php 以編輯它。記得這個文件將作為類似一個視圖文件被使用,意味着它可以包含 PHP 表達式和語句。讓我們更改模板以便生成的代碼里 attributeLabels() 方法使用 Yii::t() 來翻譯屬性標簽:
- public function attributeLabels()
- {
- return array(
- <?php foreach($labels as $name=>$label): ?>
- <?php echo "'$name' => Yii::t('application', '$label'),\n"; ?>
- <?php endforeach; ?>
- );
- }
在每個代碼模板中,我們可以訪問一些預定義的變量,例如上面例子中的 $labels 。這些變量由對應的代碼生成器提供。不同的代碼生成器可能在他們的代碼模板中提供不同的變量。請認真閱讀默認代碼模板中的描述。
現在打開 model 代碼生成器頁面。點擊 Code Template 輸入框。我們應當看到一個下拉列表 ,這個列表包含了我們新建的模板目錄 customer。我們選擇此模板生成代碼文件。
框架生成Controller的模板為:framework/gii/generators/controller/templates/default/controller.php
框架生成Model的模板為:framework/gii/generators/model/templates/default/model.php
(高級 )創建新的生成器
在framework/gii/generators創建widget文件夾,可以編寫支持module的Crud Generator,moduleID下model class的寫法application.modules.moduleID.models.modelClass 
可以把自己擴展的gii放到項目目錄下面,配置如下
- 'modules' => array(
- 'gii' => array(
- 'class' => 'system.gii.GiiModule',
- 'password' => 'gii',
- 'generatorPaths' => array(
- 'application.gii.generators',//項目目錄結構
- ),
- 'ipFilters' => array('127.0.0.1', '::1'),
- ),
- ),
上面的配置告訴 Gii在別名是application.gii.generators的目錄中尋找生成器,以及默認的framework位置 system.gii.generators在不同的搜索路徑有同名的生成器也是可以的。這種情況下,在 GiiModule::generatorPaths 指定目錄中先出現的生成器有優先權 。

