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 内容为: 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');
//配置地区 set_include_path('.' .PATH_SEPARATOR
.'./library'.PATH_SEPARATOR .'./application/models/'.PATH_SEPARATOR .
get_include_path()); //配置环境路径 //require_once
'Zend/Loader.php'; //Zend_Loader::registerAutoload();//设置Zend
Framework 自动载入类文件 require_once
"Zend/Loader/Autoloader.php";
//载入zend框架 Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);
//静态载入自动类文件 $registry
= Zend_Registry::getInstance(); //静态获得实例 $view =
new Zend_View(); //实例化zend 模板 $view->setScriptPath('./application/views/scripts/');//设置模板显示路径 $registry['view']
= $view;//注册View //设置控制器 $frontController
=Zend_Controller_Front::getInstance(); $frontController->setBaseUrl('/zendframework')//设置基本路径 ->setParam('noViewRenderer',
true) ->setControllerDirectory('./application/controllers') ->throwExceptions(true) ->dispatch(); ?> 5.zendframework\application\controllers 新建IndexController.php 内容 <?php class IndexController extends Zend_Controller_Action { function
init() //__construct 代替初始化函数 { $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与上面文件名对应 内容 <?php print_r($this->php);//此处的php与indexAction()中对应 ?> 9.测试模板 浏览器中分别输入http://localhost/zendframework/、http://localhost/zendframework/index、 http://localhost/zendframework/index/index 测试结果应该一样 10.建立自己的Action方法 打开zendframework\application\controllers\IndexController.php 在类IndexController下 建立 function myAction(){//注意大小写 echo "this is myAction"; } 11.测试 浏览器中输入http://localhost/index/my 显示结果this is myAction表示正确 小结: ①URL的第一个部份会映射到一个控制器,第二个部份则映射到控制器类中的Action(即控制器类内部的一个方法)。 ②类名包含多个单词,每个单词的第一个字母必须大写,连续的大写是不允许的 ③函数名总是以小写开头,当函数名包含多个单词,每个子的首字母必须大写 ④常量名的所有字母必须大写。 ⑤对于只包含有 PHP 代码的文件,结束标志("?>")是不允许存在的,PHP自身不需要("?>"), 这样做, 可以防止它的末尾的被意外地注入相应。 5.数据库操作 ①application文件夹下创建一个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中 //配置数据库参数,并连接数据库 $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 模块内容<?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();//实例化数据库类 //获取数据库内容
$this->view->messages=$message->fetchAll()->toArray();//将查找到的数据转换成数组存放到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
'您增加数据成功!请您<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.常用数据库函数 ☆☆fetchAll($sql):取回结果集中所有字段的值,作为连续数组返回 ☆☆fetchRow($sql):只取回结果集的第一行 fetchAssoc($sql):取回结果集中所有字段的值,作为关联数组返回 fetchCol($sql):取回所有结果行的第一个字段名 fetchOne($sql):只取回第一个字段值 fetchPairs($sql):取回一个相关数组,第一个字段值为码 第二个字段为值 插入 insert( $arrParams ) $arrParams:关联数组,
key是数据库字段,键值是字段值
return:lastInsertId返回插入的最后一行的id值 更新修改 update( $arrParams,$strSqlWhere ) $arrParams:关联数组,
key是数据库字段,通过一个where条件从句来决定需要改变的行,返回被修改的行数,自动加引号处理 $strSqlWhere:条件 $table=new RoundTable(); $db=$table->getAdapter(); $set=array('color'=>'yellow'); $where=$db->quoteInto('first_name?',值) $table->update($set,$where); 删除 delete( $strSqlWhere ) $strSqlWhere:条件返回被删除的行数 $where=$db->quoteInto('first_name?',值); $table->delete($where); 7.视图循环和条件函数,模板引擎中 foreach循环 foreach(循环条件):......endforeach;//不同处,没有大括号而是冒号,且有end if条件 if(条件):......endif; if():.... else: endif; |
|
来自: dlshanghai > 《Zend Framework》