天天看點

Zendframework

Zendframework

安裝步驟:

1. 從  Zend Framework 的網頁上下載下傳最新版本。解壓後,把整個目錄拷貝到一個理想的地

方,假設我們解壓存放在D:/web/ library/Zend.

2. 打開PHP.INI 檔案  修改配置檔案,以上面的配置為例,php.ini 中應有類似下面的條目:

linux  環境下  為  :  include_path = ".:/web/library"

windows環境下為  :include_path = ".;D:\ web \library"

檢視extension=php_pdo.dll前面是否有‘;’,如果有就把‘;’删除,這個目的是讓php

支援pdo擴充  (後期我們需要使用它來操作資料庫)

3. 修改apache 中的httpd.conf 配置檔案  開啟重寫子產品支援

LoadModule rewrite_module modules/mod_rewrite.so

把allowoverride none 修改為  all

<Directory />

Options FollowSymLinks

AllowOverride all

Order deny,allow

Deny from all

</Directory>

4. 建立.htaccess檔案  儲存在web目錄  寫上代碼

RewriteEngine on

#RewriteRule .* index.php

RewriteRule !\.(js|gif|jpg|png|swf|pdf|css|html|htm|rar|zip)$ index.php [NC,L]

php_flag magic_quotes_gpc off

php_flag register_globals off

5. 到目前為止,zendframework已經配置完畢

現在我們來了解下zendframework 項目的目錄結構

認識web目錄結構

web

application

config(系統配置檔案)

config.ini

controllers(系統控制檔案-)

models(模型檔案-一些類檔案)

views(存放視圖檔案-模闆檔案)

scripts

index

helpers

filters

library(存放zend檔案)

public (存放公共檔案)

index.php

.htaccess

如果是多目錄比如有前台後背景,假設前台為default ,背景為admin

那麼目錄結構可以修改為:

modules

admin

controllers  (系統控制檔案-)

models

views

default

現在我們來看下如何使用zendframework來工作

1.首先建立一個index.php 引導程式  檔案

代碼為:

<?php

require_once "Zend/Loader/Autoloader.php"; //載入zend架構

Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true); //靜态載入自動類檔案

$filepath = '.' .PATH_SEPARATOR .'./web/models/'.PATH_SEPARATOR .'./library'.PATH_SEPARATOR .

get_include_path();

set_include_path($filepath);

//視圖檔案設定

$registry = Zend_Registry::getInstance();

$view = new Zend_View();

$view->setScriptPath('./web/views/scripts/');//設定模闆顯示路徑,

$registry['view'] = $view;//注冊View

//設定視圖路徑  ,如果是多目錄

/*

$view->setScriptPath(

array(

'default' =>'./web/modules/default/views/scripts/',

'admin' =>'./web/modules/admin/views/scripts/'

)

);

*/

//配置資料庫參數,并連接配接資料庫,如果需要資料庫

$config=new Zend_Config_Ini('./web/config/config.ini',null, true);

Zend_Registry::set('config',$config);

$dbAdapter=Zend_Db::factory($config->general->db->adapter,$config->general->db->config

->toArray());

$dbAdapter->query('SET NAMES UTF8');

Zend_Db_Table::setDefaultAdapter($dbAdapter);

Zend_Registry::set('dbAdapter',$dbAdapter);

//設定控制器

$frontController =Zend_Controller_Front::getInstance();

$frontController->addModuleDirectory('./web/controllers ');

$frontController->setBaseUrl('/')//設定基本路徑

->setParam('noViewRenderer', true)

->setControllerDirectory('./web/ controllers')

->setParam('useDefaultControllerAlways', true)

->setParam('noErrorHandler', true)

->throwExceptions(true)

->dispatch();

//多目錄設定

/*    

$frontController->setControllerDirectory(

'default' => './web/modules/default/controllers',

'admin' => './web/modules/admin/controllers'

);      

*/        

?>

2.在目錄  application/controllers/  下建立檔案  名稱為:IndexController.php  動作控制器

預設的動作控制器(Action Controller)

Zend Framework 的預設路由規則是  http://域名/控制器名/動作(方法)名。例如:

http://example.com/user/show 會被解析到名為  User 的控制器以及該控制器中定義的

show 方法。如果該方法沒有定義,則預設轉到  index  方法。

注意:在代碼中,控制器名的後面要加上  Controller,而動作名的後面要加上  Action

//require_once 'Zend/Controller/Action.php';

class IndexController extends Zend_Controller_Action

{

function init()

$this->registry = Zend_Registry::getInstance();

$this->view = $this->registry['view'];

$this->view->baseUrl = $this->_request->getBaseUrl();

}

function indexAction()

{

$welcome = "歡迎你進入PHP進階開發系列教程-ZENDFRAMEWORK";

$this->view->welcome = $welcome;

echo $this->view->render('index/index.html');

3.在application/views/scripts/index  下建立檔案,名稱為  index.html  的視圖(頁面)腳本

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title><?=$this->welcome?></title>

<style>

div{ background-color:#f1f1f1; border:1px solid #CCCCCC; text-align:center; line-height:40px;

width:300px; margin:50px auto;}

</style>

</head>

<body>

<div>

<?=$this->welcome?>

</div>

</body>

</html>

4.建立錯誤控制檔案

預設情況下,Zend Framework 的錯誤處理插件是被注冊的。它需要一個錯誤控制器來處理

錯誤。預設的錯誤控制處理被假定為  ErrorController 以及其中定義的  errorAction。

編輯  application/controllers/ErrorController.php,輸入:

/** Zend_Controller_Action */

require_once 'Zend/Controller/Action.php';

class ErrorController extends Zend_Controller_Action

public function errorAction()

5.下面是對應的視圖腳本。編輯  application/views/scripts/error/error.phtml,輸入:

<!DOCTYPE htmlPUBLIC "-//W3C//DTD XHTML 1.0

Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>  

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>錯誤</title>

<body>

<h1>錯誤</h1>

6.運作

好,現在運作網站。在浏覽器中鍵入下面三個位址,得到的結果應該是一樣的——就是最最

常見的“Hello, World!“。

http://域名

http://域名/index

http://域名/index/index

如果是這樣,那麼恭喜你  以上就是zendframework的配置過程

下面我們将要來學習zendframework 資料庫連接配接操作,多目錄的操作。

Zendframework--------資料庫操作

現在我們回頭看下入口檔案index.php

裡面有段代碼我們之前注釋掉的

現在我們要把這段代碼注釋去掉

我們需要在web/config/下建立一個檔案  為  config.ini

并在裡面寫入代碼

[general]

db.adapter=PDO_MYSQL

db.config.host =localhost

db.config.username =root

db.config.password= 123456

db.config.dbname=zend

PDO_mysql為資料庫操作對象

db.config.host   為資料庫主機

db.config.username   為資料庫使用者名

db.config.password   為資料庫密碼

db.config.dbname   為需要操作的資料庫

資料庫檔案配置寫完,現在來看下資料的操作

我們已經建立了MVC中的V和C,現在我們就開始建立M

在資料庫中建立一個表  zend

id 為主鍵,自動增長

dbAdapter  類型操作資料

在indexController.php    init(方法中增加代碼)

//資料庫dbAdapter操作

$this->db = $this->registry['dbAdapter'];

同時新增mysqlAction方法

Function mysqlAction(){

你可以通過浏覽器開始通路這個方法:http://域名/index/mysql

1.CODE:

//添加引号防止資料庫攻擊

//第一種是quote() 方法. 該方法會根據資料庫adapter為标量加上 合适的引号;

假如你試圖對一個數組做quote操作, 它将為數組中 每個元素加上引号,并用","分隔傳回

$db = $this->db;

// 為标量加引号

$value = $db->quote('St John"s Wort');

//$value 現在變成了 '"St John\"s Wort"' (注意兩邊的引号)

echo $value;

2.CODE

// 為數組加引号

$value = $db->quote(array('a', 'b', 'c'));

// $value 現在變成了 '"a", "b", "c"' (","分隔的字元串)

print_r($value);

3.CODE:

//第二種是 quoteInto() 方法. 你提供一個包含問号占 位符的基礎字元串 , 然

後在該位置加入帶引号的标量或者數組. 該 方法對于随需建構查詢sql語句和條件語句是

很有幫助的. 使用 quoteInto處理過的标量和數組傳回結果與quote() 方法相同

// 在where語句中為标量加上引号

$where = $db->quoteInto('id = ?', 1);

// $where 現在為 'id = "1"' (注意兩邊的引号)

Echo $where;

4.CODE:

// 在where語句中為數組加上引号

$where = $db->quoteInto('id IN(?)', array(1, 2, 3));

// $where 現在為 'id IN("1", "2", "3")' (一個逗号分隔的字元串)

5.CODE:

//直接查詢

// 使用完整的sql語句直接進行查詢.

$sql = $db->quoteInto(

'SELECT * FROM zend WHERE id > ?',

'1'

$result = $db->query($sql);

// 使用PDOStatement對象$result将所有結果資料放到一個數組中

$rows = $result->fetchAll();

print_r($rows);

6.CODE:

//你可以将資料自動的綁定到你的查詢中。這意味着你在查詢中可以設定 多個指定的

占位符,然後傳送一個數組資料以代替這些占位符。這些替 換的資料是自動進行加引号處

理的,為防止資料庫攻擊提供了更強的安 全性

$result = $db->query(

'SELECT * FROM zend WHERE id > :placeholder',

array('placeholder' => '1')

7.CODE:

//你也可以手工設定sql語句和綁定資料到sql語句。這一功能通過 prepare() 方

法得到一個設定好的PDOStatement對象,以便直 接進行資料庫操作.

// 這次, 設定一個 PDOStatement 對象進行手工綁定.

$stmt = $db->prepare('SELECT * FROM zend WHERE id > :placeholder');

$stmt->bindValue('placeholder', '3');

$stmt->execute();

$rows = $stmt->fetchAll();

8:CODE

//插入資料

//可以使用 insert()方法将要插入的資料綁定并建立 一個insert語句(綁定的數

據是自動進行加引号處理以避免資料庫攻擊的)

//傳回值并 不是 最後插入的資料的id,這樣做的原因在于一些表 并沒有一個自增

的字段;相反的,這個插入的傳回值是改變的資料行數(通常情況為1)。 假如你需要最後插入

的資料id,可以在insert執行後調用 lastInsertId() 方法。

//

// INSERT INTO zend

// (username)

// VALUES ('123123');

// 以"列名"=>"資料"的格式格式構造插入數組,插入資料行

$row = array (

'username'  => time(),

// 插入資料的資料表

$table = 'zend';

// i插入資料行并傳回行數

$rows_affected = $db->insert($table, $row);

$last_insert_id = $db->lastInsertId();

echo $last_insert_id;

9:CODE

//更新資料

//可以使用 update() 方法确定需要update的資料并且建立一個 update語句(确

定的資料是自動加引号處理以避免資料庫攻擊的)。

//你可以提供一個可選的where語句說明update的條件(注意:where語句并 不是

一個綁定參數,是以你需要自己資料進行加引号的操作)。

// UPDATE zend

// SET username = "zppx"

// WHERE id = "20";

// 以"列名"=>"資料"的格式構造更新數組,更新資料行

$set = array (

'username' => 'zppx',

// 更新的資料表

// where語句

$where = $db->quoteInto('id = ?', '20');

// 更新表資料,傳回更新的行數

$rows_affected = $db->update($table, $set, $where);

10:CODE

//删除資料

//可以使用 delete() 方法建立一個delete語句;你 也可以提供一個where語句

以說明資料的删除條件。(注意:where語句并不是一個綁 定參數,是以你需要自己進行資料

加引号處理)。

// 需要删除資料的表

// WHERE username = "zppx";

// 建立一個 $db對象, 然後...

// 設定需要删除資料的表

// where條件語句

$where = $db->quoteInto('username = ?', 'zppx');

// 删除資料并得到影響的行數

$rows_affected = $db->delete($table, $where);

11:CODE

//取回查詢結果

//可以使用query()方法直接對資料庫進行操作,但是通常情況 下,仍然還是需要

選擇資料行并傳回結果。以fetch開頭的一系列的 方法可以實作這個要求。對于每一種 fetch

系列 的方法來說,你需 要傳送一個select的sql語句;假如你在操作語句中使用指定的占位

符,你也可以 傳送一個綁定資料的數組對你的操作語句進行處理和替換。 Fetch系列 的方法

包括

//fetchAll()

// 取回結果集中所有字段的值,作為連續數組傳回

$result = $db->fetchAll(

"SELECT * FROM zend WHERE username = :title",

array('title' => 'zppx')

//fetchAssoc()

// 取回結果集中所有字段的值,作為關聯數組傳回

// 第一個字段作為碼

$result = $db->fetchAssoc(

//fetchCol()

// 取回所有結果行的第一個字段名

$result = $db->fetchCol(

"SELECT username FROM round_table WHERE username = :title",

//fetchOne()

// 隻取回第一個字段值

$result = $db->fetchOne(

"SELECT COUNT(*) FROM zend WHERE username = :title",

//fetchPairs()

// 取回一個相關數組,第一個字段值為碼

// 第二個字段為值

$result = $db->fetchPairs(

"SELECT username, id FROM zend WHERE username = :title",

//fetchRow()

// 隻取回結果集的第一行

$result = $db->fetchRow(

"SELECT * FROM zend WHERE username = :name",

array('name' => 'zppx')

以上都是在控制檔案中操作的,現在我們來看下在models中是如何操作資料庫的

我們在models中建立一個檔案 名稱為:Zenduser.php

class Zenduser extends Zend_Db_Table

protected $_name ="zend"; // 預設表為'class_name'你可以通過這個參數修改

protected $_primary = 'id';//預設主鍵  為’id’ 你可以通過這個參數修改

function init() {

$this->_db = $this->getAdapter();//擷取資料庫操作

parent::init();

注意:類名稱和檔案名稱保持一緻

在indexController.php檔案中增加一個方法,modelAction

Function modelAction(){

現在我們來完善這個方法,通過代碼來了解

$Zenduser = new Zenduser();

//如果是直接調用models中的類成員,則新加資料 隻需要将列名:資料的關聯數組作為參數,

調 用insert()方法即可.(zend framework)會自動對資料進行加引号處理, 并傳回插入的

最後一行的id值

//插入

// VALUES ("zppx")

$data = array(

'username' => 'zppx'

$id = $Zenduser->insert($data);

echo $id;

//要修改表中的任意行資料,我們可以設定一個列名:資料的關聯數組作為參數,調

用update()方法,同是通過一個where條件從句來決定需要改變的行.該方法将會 修改表中數

據并傳回被修改的行數.

// SET username = "szzppx"

// WHERE id = "23"

$set = array(

'username' => 'szzppx',

$where = $db->quoteInto('id = ?', '23');

$rows_affected = $Zenduser->update($set, $where);

//要删除表中的資料,我們可以調用delete()方法,同時通過一個where條件 分句

來決定需要删除的行.該方法将會傳回被删除的行數

//(zend framework)不會對條件分句進行加引号處理,是以你需要使用該表 的

zend_db_adapter對象完成該工作.

// DELETE FROM zend

// WHERE username = "szzppx"

$where = $db->quoteInto('username = ?', 'szzppx');

$rows_affected = $Zenduser->delete($where);

//根據主鍵查找資料

//通過調用find()方法,可以使用主鍵值輕松地在表中檢索資料.假如你隻想要查詢

某 一條資料,該方法将回傳回一個zend_db_table_row對象,而當你想要查詢多條記錄時 ,

将會傳回一個zend_db_table_rowset對象

// SELECT * FROM zend WHERE id = "1"

$row = $Zenduser->find(1);

// SELECT * FROM zend WHERE id IN("1", "2", 3")

$rowset = $Zenduser->find(array(1, 2, 3));

//取回一條記錄,不按主鍵

// SELECT * FROM zend

// WHERE username = "zppx"

// AND sex = "1"

// ORDER BY id desc

$where = $db->quoteInto('username = ?', 'zppx')

. $db->quoteInto('AND sex = ?', '1');

$order = 'id';

$row = $Zenduser->fetchRow($where, $order);

//取回多條記錄

//假如需要一次檢索多條記錄.可以使用fetchAll()方法.和使用fetchRow()方

法類 似,該方法不僅僅可以設定where和order分句,也可以設定limit-count和

limit-offset值來限制傳回的結果數.執行該方法後,把選擇的結果作為一個

Zend_Db_Table_Rowset對象傳回.

//注意,(zend framework) 将不會對where語句進行加引号處理,是以你需要 通

過zend_db_adapter進行資料處理

// ORDER BY id

// LIMIT 10 OFFSET 20

$count = 10;

$offset = 20;

$rowset = $Zenduser->fetchAll($where, $order, $count, $offset);

現在我們來看下如何在models中如何操作資料庫代碼

在Zenduser.php中增加一個方法

function inserDate(){

//查詢

$result = $this->_db->fetchPairs(

print_r($result);

我們繼續完善控制檔案

增加代碼

上一篇: jquery
下一篇: cluster(1)

繼續閱讀