|
1.php開啟PDO php.ini ①開啟extension=php_pdo.dll ②開啟extension=php_pdo_mysql.dll 去掉之前; 2.apache開啟rewrite模塊 httpd.conf ①開啟rewrite_module LoadModule rewrite_module modules/mod_rewrite.so ②開啟.htaccess目錄識別AllowOverride None改成All <Directory /> Options
FollowSymLinks AllowOverride None Order deny,allow Deny from all Satisfy all </Directory> None改為all ③AllowOverride None改為All注意共有兩處 ④重啟apache 3.在zendframework下新建文件.htaccess 內(nèi)容為: RewriteEngine on RewriteRule .* index.php php_flag magic_quotes_gpc off php_flag register_globals off 4.在zendframework下新建文件index.php <?php error_reporting(E_ALL|E_STRICT);
//在開啟錯誤報告 date_default_timezone_set('Asia/Shanghai');
//配置地區(qū) set_include_path('.' .PATH_SEPARATOR
.'./library'.PATH_SEPARATOR .'./application/models/'.PATH_SEPARATOR .
get_include_path()); //配置環(huán)境路徑 //require_once
'Zend/Loader.php'; //Zend_Loader::registerAutoload();//設(shè)置Zend
Framework 自動載入類文件 require_once
"Zend/Loader/Autoloader.php";
//載入zend框架 Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);
//靜態(tài)載入自動類文件 $registry
= Zend_Registry::getInstance(); //靜態(tài)獲得實例 $view =
new Zend_View(); //實例化zend 模板 $view->setScriptPath('./application/views/scripts/');//設(shè)置模板顯示路徑 $registry['view']
= $view;//注冊View //設(shè)置控制器 $frontController
=Zend_Controller_Front::getInstance(); $frontController->setBaseUrl('/zendframework')//設(shè)置基本路徑 ->setParam('noViewRenderer',
true) ->setControllerDirectory('./application/controllers') ->throwExceptions(true) ->dispatch(); ?> 5.zendframework\application\controllers 新建IndexController.php 內(nèi)容 <?php class IndexController extends Zend_Controller_Action { function
init() //__construct 代替初始化函數(shù) { $this->registry =
Zend_Registry::getInstance(); $this->view
= $this->registry['view'];
$this->view->baseUrl = $this->_request->getBaseUrl(); } /* * Action(動作)! */ function
indexAction() { echo "測試成功";} } 6.瀏覽器中打開http://localhost/zendframework、http://localhost/zendframework/index、 http://localhost/zendframework/index/index均顯示測試成功。 否則查看apache配置中的AllowOverride None 7.通過視圖測試 打開zendframework\application\controllers\IndexController.php 修改 function indexAction() { echo "測試成功";} 改為 function indexAction() { $a="視圖測試"; $this->view->php=$a; echo $this->view->render('index.phtml');//顯示模版 } 8.建立模板 zendframework\application\controllers\views\scripts 新建文件index.phtml與上面文件名對應 內(nèi)容 <?php print_r($this->php);//此處的php與indexAction()中對應 ?> 9.測試模板 瀏覽器中分別輸入http://localhost/zendframework/、http://localhost/zendframework/index、 http://localhost/zendframework/index/index 測試結(jié)果應該一樣 10.建立自己的Action方法 打開zendframework\application\controllers\IndexController.php 在類IndexController下 建立 function myAction(){//注意大小寫 echo "this is myAction"; } 11.測試 瀏覽器中輸入http://localhost/index/my 顯示結(jié)果this is myAction表示正確 小結(jié): ①URL的第一個部份會映射到一個控制器,第二個部份則映射到控制器類中的Action(即控制器類內(nèi)部的一個方法)。 ②類名包含多個單詞,每個單詞的第一個字母必須大寫,連續(xù)的大寫是不允許的 ③函數(shù)名總是以小寫開頭,當函數(shù)名包含多個單詞,每個子的首字母必須大寫 ④常量名的所有字母必須大寫。 ⑤對于只包含有 PHP 代碼的文件,結(jié)束標志("?>")是不允許存在的,PHP自身不需要("?>"), 這樣做, 可以防止它的末尾的被意外地注入相應。 5.數(shù)據(jù)庫操作 ①application文件夾下創(chuàng)建一個config文件夾 然后建立config.ini,不要加引號,注意下面不能加注釋 [general] db.adapter=PDO_MYSQL db.config.host=localhost db.config.username=root db.config.password= db.config.dbname=zend ②配置文件引入到zendframework中在index.php中 //配置數(shù)據(jù)庫參數(shù),并連接數(shù)據(jù)庫 $config=new
Zend_Config_Ini('./application/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); ③建模塊applications/models/,模塊名和類名一致 Message 模塊內(nèi)容<?php class Message extends Zend_Db_Table { protected
$_name ="message";//表名 protected
$_primary = 'id';//主鍵 } ?> ④控制器 <?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() { $message=new message();//實例化數(shù)據(jù)庫類 //獲取數(shù)據(jù)庫內(nèi)容
$this->view->messages=$message->fetchAll()->toArray();//將查找到的數(shù)據(jù)轉(zhuǎn)換成數(shù)組存放到messages中 echo
$this->view->render('index.phtml');//顯示模版 } function addAction(){ //如果是POST過來的值.就增加.否則就顯示增加頁面 if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){ $content=$this->_request->getPost('content'); $title=$this->_request->getPost('title'); $message=new
Message(); $data=array( 'content'=>$content, 'title'=>$title ); $message->insert($data); unset($data); echo
'您增加數(shù)據(jù)成功!請您<a
href="'.$this->view->baseUrl.'/index/index/">返回</a>'; }else{ echo $this->view->render('add.phtml');//顯示增加模版 } } } ⑤views/scripts/index.phtml <?php foreach($this->messages as $message): ?> <tr> <th><?php echo $message['id'];
?></th> <td><?php
echo $message['name']; ?></td> </tr> <?php endforeach; ?> 6.常用數(shù)據(jù)庫函數(shù) ☆☆fetchAll($sql):取回結(jié)果集中所有字段的值,作為連續(xù)數(shù)組返回 ☆☆fetchRow($sql):只取回結(jié)果集的第一行 fetchAssoc($sql):取回結(jié)果集中所有字段的值,作為關(guān)聯(lián)數(shù)組返回 fetchCol($sql):取回所有結(jié)果行的第一個字段名 fetchOne($sql):只取回第一個字段值 fetchPairs($sql):取回一個相關(guān)數(shù)組,第一個字段值為碼 第二個字段為值 插入 insert( $arrParams ) $arrParams:關(guān)聯(lián)數(shù)組,
key是數(shù)據(jù)庫字段,鍵值是字段值
return:lastInsertId返回插入的最后一行的id值 更新修改 update( $arrParams,$strSqlWhere ) $arrParams:關(guān)聯(lián)數(shù)組,
key是數(shù)據(jù)庫字段,通過一個where條件從句來決定需要改變的行,返回被修改的行數(shù),自動加引號處理 $strSqlWhere:條件 $table=new RoundTable(); $db=$table->getAdapter(); $set=array('color'=>'yellow'); $where=$db->quoteInto('first_name?',值) $table->update($set,$where); 刪除 delete( $strSqlWhere ) $strSqlWhere:條件返回被刪除的行數(shù) $where=$db->quoteInto('first_name?',值); $table->delete($where); 7.視圖循環(huán)和條件函數(shù),模板引擎中 foreach循環(huán) foreach(循環(huán)條件):......endforeach;//不同處,沒有大括號而是冒號,且有end if條件 if(條件):......endif; if():.... else: endif; |
|
|