Zend framework

Zend framework框架
開發web程序和服務,ZF採用MVC架構模式來分離應用程序下。
PHP5面向對象描寫:豐富完善的組件支持,Ajax支持php

OOP MVChtml

環境需求和配置
php版本須要在5.2.0以上(Wamp)web

php須要開啓PDO和PDO相關數據庫引擎來運行ZF:extension=php_pdo.dll以前的分號去掉sql

Apache 開啓rewrite_module模塊,並開啓.htaccess目錄AllowOveride ALL數據庫

ZF框架的搭配與調試:
http://www.zendframework.com/download/latest
------application MVC文件夾
------------controllers 控制文件夾
------------models 模塊文件夾
------------views 模板文件夾
----------------filters
----------------helpers
----------------scripts
------library
-------------Zend
-----public 公共配置文件夾
-------------images
-------------scripts
-------------styles
-----.htaccess
-----.project
-----index.php

index.php 配置文件,單一入口。數組

能夠下載一個Zend Framework的核心板,而後複製出library文件夾
init()代替__construct,進行優化,使用更加方便架構

Index控制器中 $this->view->word = '測試一個內容';
========================================
php100:76:Zend Framework 數據庫操做app

Zend framework 如何寫數據庫配置文件
在application 文件夾下面建立一個config文件夾,而後創建一個config.ini文件,以下圖所示:
[general]
db.adapter=PDO_MYSQL
db.config.host=localhost
db.config.username=root
db.config.password=
db.config.dbname=zend框架

配置文件引入到Zend Framework
$config=new Zend_Config_ini('./application/config/config.ini',null,true);
Zend_Registry::set('config',$config);ide

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

如何控制和處理Zend Framework方法內容
採用單一入口模式,全部後面跟的參數須要 /來區別
localhost/zendframework/index/add
function addAction(){}
IndexActionController.php
經過這樣的方式訪問了index模塊下的add方法,系統默認的是indexAction()索引方法名。

=================================
php100:77:Zend Framework數據庫之修改和視圖函數

Zend Framework Db_Table經常使用函數介紹:
fetchAll($sql):取回結果集中全部字段的值,做爲連續數組返回
fetchRow($sql):只返回結果集中的一行
fetchAssoc($sql):取回結果集中全部字段的值,做爲關聯數組返回
fetchCol($sql):取回全部結果航的第一個字段名
fetchOne($sql):只取回第一個字段值
fetchPairs($sql):取回相關數組,第一個是Key值,第二個是值

insert($arrParams)
關聯數組,key是數據庫字段
update($arrParams,$steSqlWhere)
delete($steSqlWhere)

insert()插入數據
insert()方法,列名:數據的關聯數組做爲參數,自動加引號處理,並返回插入的最後一行的id值
$data=array{
'name'=>'King';
'color'=>'blue';
}
$id=$table->insert($data);

update()
方法,列名;數據的關聯數組做爲參數。
$table=new RoundTable();
$db=$table->getAdapter();
$set=array{
'color'=>'yellow';
}
$where =$db->quoteInto('first_name=?','Robin');
$table->update($set,$where);

delete()
$where=$db->quoteInto('first_name=?','Robin');
$table->delete($where);


Zend Framework視圖循環和條件函數:
Foreach 循環
Foreach(循環條件) :....endforeach;
IF
if()......endif;

 =======================================

MVC 代碼書寫:
控制器代碼書寫:
<?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()
{
$this->view->word=" I love spurs";

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

}
function addAction(){
//若是是POST過來的值.就增長.不然就顯示增長頁面


}
}
?>
控制當中寫內容:$this->view->word="ggg";
$this->view->render("index.html");
---->index.html echo $this->word;

application->config.ini
[general]
db.adapter=PDO_MYSQL
db.config.host=localhost
db.config.username=root
db.config.password=
db.config.dbname=think_zw

配置文件引入到framework裏面去
//配置數據庫參數,並鏈接數據庫
$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);

單一入口模式:localhost/index/add/訪問index模塊下的add方法
function addAction(){}(在IndexController.php)
默認訪問爲index模塊下的index方法

再創建一個模塊model裏面的message.php
<?php
class Message extends Zend_Db_Table
{
protected $_name ="message";
protected $_primary = 'id';
}
?>
模塊實例化:
function indexAction()
{
$message=new message();//實例化數據庫類

//獲取數據庫內容
$this->view->messages=$message->fetchAll()->toArray();

echo $this->view->render('index.phtml');//顯示模版
}

<?foreach($this->messages as $message): ?>
<tr>
<th><?php echo $message['title']; ?></th>
<td><?php echo $message['content']; ?></td>
</tr>
<?endforeach; ?>


*************
修改和刪除數據

<?php if(2==2):?>
kk
<?php else:?>
ll
<?php endif;?>

index.phtml裏面加上<a href="<?php echo $this->baseUrl?>/index/exit">編輯</a>
<a href="<?php echo $this->baseUrl?>/index/delete">刪除</a>

添加一個新的方法:edit.phtml
function editAction(){

$message = new Message();
$db = $message->getAdapter();

if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
$id = $this->_request->getPost('id');
$cid = $this->_request->getPost('cid');
$title = $this->_request->getPost('title');

$set = array(
'cid'=>$cid,
'title'=>$title
);
$where = $db->quoteInto('id = ?',$id);
//更新數據
$message->update($set,$where);
unset($set);
echo '修改數據成功!<a href="'.$this->view->baseUrl.'/index/index/">返回</a>';
}else{
$id = $this->_request->getParam('id');
$this->view->messages = $message->fetchAll('id='.$id)->toArray();
echo $this->view->render('edit.phtml');
}
}


function delAction(){
$message = new Message();
$id = (int)$this->_request->getParam('id');

if($id > 0){
$where = 'id = ' . $id;
$message->delete($where);
}
echo '刪除數據成功!<a href="'.$this->view->baseUrl.'/index/index/">返回</a>';
}


異常出現:
Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (index.php)' in

解決辦法:在index.php中的
$frontController =Zend_Controller_Front::getInstance();後加上
$frontController->setParam('useDefaultControllerAlways', true);

*******id/3 等於之前的?id=3

相關文章
相關標籤/搜索