PHP架構YII劄記之1

來源:互聯網
上載者:User
PHP架構YII筆記之1
1 用腳手架安裝yiic,注意把framework下的yiic和php下的php.exe放到PATH中去,
假設應用目錄為yiidemo,則先
cd yiidemo
yiic webapp demo(這個是應用程式的名)
之後會產生架構


2 建立controller
cd yiidemo
yiic shell
此時可以help去看協助
先產生的controller
controller message(controller名) helloworld(方法名)
預設在protectd目錄下也產生controller,並且views目錄下也有同controller
名相同的views檔案

運行:
http://localhost:8082/myphp/yiidemo/index.php?r=message/helloWorld

往視圖中傳遞參數也是一樣的:
$theTime = date("D M j G:i:s T Y");
$this->render('helloWorld',array('time'=>$theTime));
VIEW中:



3 yii的HTML協助類的串連:

出來的會是一個LINK了,第一個參數是串連的文字,第2個數組是
key-value,分別是controller/method

4 yii的單元測試
測試檔案放在protected/tests/unit/中
一般是以Action+Test命名,比如MessageTest.class
,如果要測試的方法為actionHelloworld(),則測試方法為
testActionHelloworld().

功能測試檔案放在protected/tests/functional中

5 單元測試步驟
1)安裝phpunit
pear channel-discover pear.phpunit.de
pear install phpunit/PHPUnit

(注意升級下pear upgrade -all)
2)下載selenium RC SERVER
3) 修改 test目錄下的WebTestCase.php,設定為
define('TEST_BASE_URL','http://localhost:8082/myphp/yiidemo/index-

test.php/');
4) 啟動selenium server

5) 測試開始
cd protected/tests/
phpunit functional/SiteTest.php
會自動開啟IE,FIREFOX去測試,可以到test\phpunit.xml中去修改瀏覽器設定

6)TDD
每寫一個類,則
phpunit unit/XXXX.PHP測試下

例子
Yii::import('application.controllers.MessageController');
class MessageTest extends CTestCase
{
public function testRepeat()
{

$message = new MessageController('messageTest');
$yell = "Hello, Any One Out There?";
$returnedMessage = $message->repeat($yell);
$this->assertEquals($returnedMessage, $yell);
}
}

?>

7 資料庫連接
$connection=new CDbConnection($dsn,$username,$password);
SQLite: sqlite:/path/to/dbfile
? MySQL: mysql:host=localhost;dbname=testdb
? PostgreSQL: pgsql:host=localhost;port=5432;dbname=testdb
? SQL Server: mssql:host=localhost;dbname=testdb
? Oracle: oci:dbname=//localhost:1521/testdb
設定檔在
/protected/config/main.php
修改為MYSQL配置
// application components
'components'=>array(
…
'db'=>array(
'connectionString' => 'mysql:host=127.0.0.1;dbname=trackstar_dev',
'emulatePrepare' => true,
'username' => 'your_db_user_name',
'password' => 'your_db_password',
'charset' => 'utf8',
),
),
使用時:Yii::app()->db


8 啟動GII
gii是快速構建的工具之一,實際上就是一個網頁的腳手架產生工具
1)配置
protected\config\main.php
增加:
'modules'=>array(
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>'[自己定的密碼]',
),
),


2) 在資料庫中建立表後,到GII中產生MODEL類
3)繼續單元測試
class ProjectTest extends CDbTestCase
{
public function testCRUD()
{
//Create a new project
$newProject=new Project;
$newProjectName = 'Test Project 1';
$newProject->setAttributes(
array(
'name' => $newProjectName,
'description' => 'Test project number one',
'create_time' => '2010-01-01 00:00:00',
'create_user_id' => 1,
'update_time' => '2010-01-01 00:00:00',
'update_user_id' => 1,
)
);
$this->assertTrue($newProject->save(false));
}
}
?>
其中save傳遞的false參數,是說儲存時不再驗證合法性了
測試讀:
//READ back the newly created project
$retrievedProject=Project::model()->findByPk($newProject->id);
$this->assertTrue($retrievedProject instanceof Project);
$this->assertEquals($newProjectName,$retrievedProject->name);
$newProject->id找出其ID主鍵
更新及刪除
//UPDATE the newly created project
$updatedProjectName = 'Updated Test Project 1';
$newProject->name = $updatedProjectName; $this-
>assertTrue($newProject->save(false));
//read back the record again to ensure the update worked
$updatedProject=Project::model()->findByPk($newProject->id);
$this->assertTrue($updatedProject instanceof Project);
$this->assertEquals($updatedProjectName,$updatedProject->name);
//DELETE the project
$newProjectId = $newProject->id;
$this->assertTrue($newProject->delete());
$deletedProject=Project::model()->findByPk($newProjectId);
$this->assertEquals(NULL,$deletedProject);


再用腳手架的crud generator,於是就CRUD都可以產生了。




9 validator
YII有很多validator,用法:
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('create_user_id, update_user_id', 'numerical','integerO
nly'=>true),
array('name', 'length', 'max'=>128),
array('create_time, update_time', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, name, description, create_time, create_user_id,
update_time, update_user_id', 'safe', 'on'=>'search'),
array('name', 'required'),
);
}
'on'=>'insert'之類的,是說在INSERT的情況下,規則適合用
array('name','欄位’,'欄位2'......'required'),


10 建立TEST FIXTURE
1)protected/tests/fixtures/tbl_project.php,檔案都在

protected/tests/fixtures下建立
比如
return array(
'project1'=>array(
'name' => 'Test Project 1',
'description' => 'This is test project 1',
'create_time' => '',
'create_user_id' => '',
'update_time' => '',
'update_user_id' => '',
),
'project2'=>array(
'name' => 'Test Project 2',
'description' => 'This is test project 2',
'create_time' => '',
'create_user_id' => '',
'update_time' => '',
'update_user_id' => '',
),
'project3'=>array(
'name' => 'Test Project 3',
'description' => 'This is test project 3',
'create_time' => '',
'create_user_id' => '',
'update_time' => '', 'update_user_id' => '',
),
);
之後,在tests/unit/ProjectTest.php中建立:
class ProjectTest extends CDbTestCase
{
public $fixtures=array
(
'projects'=>'Project',
);
}
告訴我們去使用剛才的fixture.
使用測試資料
$projectOne = $this->projects['project1'];


11) 在測試時,使用測試資料庫
在protected/config/test.php中,如下設定:
return CMap::mergeArray(
require(dirname(__FILE__).'/main.php'),
array(
'components'=>array(
'fixture'=>array(
'class'=>'system.test.CDbFixtureManager',
),
'db'=>array(
'connectionString' =>
'mysql:host=localhost;dbname=trackstar_test',
'emulatePrepare' => true,
'username' => '[your db username]',
'password' => '[your db password]',
'charset' => 'utf8',
),
),
)

);
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.