php利用zendframework编程实例
来源:互联网 发布:淘宝免费送衣服返现 编辑:程序博客网 时间:2024/06/06 09:16
本文章参考《php顶级框架zendframe开发实战》第四章内容,并完整实现...
首先将用到的css文件下载下来:http://download.csdn.net/download/unityoxb/4058802
解压后把default和common两个文件复制到public/skins目录下;
/layout/scripts/layout.phtml
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="skins/default/css/layout.css" rel="stylesheet" type="text/css" /> <link href="skins/default/css/style.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="wrap"> <?php echo $this->partial('header.phtml'); ?> <div id="main"> <div id="sidebar"></div> <div id="content"> <?php echo $this->layout()->content ?> </div> </div> <?php echo $this->partial('footer.phtml') ?> </div> </body> <!-- <?php echo $this->layout()->content ?> -\-> --></html>
1、用到的数据库文件mysql.sql
create table if not exists `core_pages`( `id` int(10) unsigned not null auto_increment comment '页面唯一ID', `cid` int(10) unsigned not null default '0' comment '分类ID', `uid` int(10) unsigned not null default '0' comment '用户ID', `title` varchar(255) not null comment '页面标题', `body` text not null comment '内容', `status` tinyint(4) not null default '1' comment '是否发布', `createtime` int(11) not null default '0' comment '创建页面时间', `updatetime` int(11) not null default '0' comment '修改页面时间', `comment` tinyint(4) not null default '0' comment '页面是否评论功能', `start` tinyint(4) not null default '0' comment '页面级别', `top` tinyint(4) not null default '0' comment '置顶', primary key (`id`))ENGINE=InnoDB default charset=utf8;
打开mysql,使用source mysql.sql即可创建表结构
2、配置application.ini文件(hahacom/applicaton/configs)
主要配置zend Framework连接mysql
[development : production]phpSettings.display_startup_errors = 1phpSettings.display_errors = 1resources.frontController.params.displayExceptions = 1resources.db.adapter = "PDO_MYSQL"resources.db.params.host = "localhost"resources.db.params.username = "root"resources.db.params.password = "root"resources.db.params.dbname = "test" --这是数据名称resources.db.isDefaultTableAdapter = "TRUE"resources.db.params.driver_options.1002 = "SET NAMES UTF8;"
3、public/index.php
// Define application environmentdefined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : '<strong>development</strong>')); //修改成测试环境
4、创建文章展示model(model主要存储数据模型类似javabean,或者从数据库获取数据并保存在内存里)
执行命令:zf create model page
会自动生成一个models/Page.php文件
<?phpclass Application_Model_Page{ protected $_name = 'core_pages'; public $result; public function getPage($where = array()) { $db = Zend_Db_Table::getDefaultAdapter(); // $db = $this->getAdapter(); $select = $db->select(); /*if($where != null) { //$select->where(' star = ? ', $where); //$sql = $db->quoteInto("select * from `core_pages` where `star`= ?", $where); //$result = $db->query($sql); $select->from('core_pages','*')->where('star = ?', $where)->limit(1); }*/ $select->from('core_pages','*'); if(count($where)>0) { foreach($where as $key=>$value) $select->where($key.' = ?',$value); } //$row = $result->fetch(); $row = $db->fetchAll($select); if($row) { return $row; } else { echo "================="; return null; } } public function getPages($where = null) { $db = Zend_Db_Table::getDefaultAdapter(); if(is_numeric($where)) { //$row = $db->find($where)->current(); $select = $db->select(); $select->from('core_pages','*'); $select->where('id = ?', $where); $row = $db->fetchRow($select); } if(is_array($where) && count($where)>0) { $select = $db->select(); $select->from('core_pages','*'); foreach($where as $key=>$value){ $select->where($key.'=?', $value); } $row = $db->fetchAll($select); } if($row) { return $row; } else { echo "================="; return null; } }}?>
5、创建controller
执行命令: zf create controller news 会自动生成controllers/NewsController.php
<?phpclass NewsController extends Zend_Controller_Action{ public function init() { /* Initialize action controller here */ } public function indexAction() { // action body $modelPage = new Application_Model_Page(); //$star = 1; $where = array('top'=>1, 'comment'=>1); $newsStar = $modelPage->getPage($where); //print_r($newsStar); $this->view->News = $newsStar; //$this->view->name = "hahaha"; }}
执行命令: zf create controller page 和 zf create action detail page
会自动生成 controllers/PageController.php
<?phpclass PageController extends Zend_Controller_Action{ public function init() { /* Initialize action controller here */ } public function indexAction() { // action body } public function detailAction() { // action body $id = $this->_request->getParam('id'); $modelPage = new Application_Model_Page($id);//if($modelPage == null) //print_r('==============================');//print_r($id);//print_r($modelPage); $page = $modelPage->getPages($id); $this->view->page = $page; }}
5、接下来创建视图文件
/views/scripts/news/index.phtml
<?phpecho "<h3>".$this->News[0]['title']."</h3>";echo $this->News[0]['body'];//echo $this->name;if($this->News){ /*echo "<ul>"; // print_r($this->News); foreach($this->News as $val) { echo "<li>"."<u>".$val['title']."</u>"."</li>"; } echo "</ul>"; */ echo "<ul class = 'listNews'>"; echo $this->partialLoop('row_pages.phtml', $this->News); echo "</ul>";}?>
/views/scripts/row_pages.phtml
<li> <a href = "/page/detail/id/<?php echo $this->id; ?>"><?php echo $this->title; ?></a> 发表时间: <?php echo date('Y-m-d', $this->createtime); ?></li>
/views/scripts/page/detail.phtml
<?php echo "<h2>".$this->page['title']."</h2>"; echo "发表:".date('Y-m-d', $this->page['createtime']).""; echo "<hr/>"; echo $this->page['body'];?>
运行截图:
点击链接:
0 0
- php利用zendframework编程实例
- php-zendframework笔记
- ZendFramework,php编码规范
- 【php+zendframework】调试
- PHP框架学习2——ZendFramework的介绍、安装和实例运行
- PHP ZendFramework URL路由自定义
- 【zendframework】ZendFramework的介绍、安装和实例运行
- php socket编程实例
- OOP编程php实例
- php socket编程实例
- zendframework
- php://filter漏洞利用实例
- (一)zendframework blog实例分析
- ZendFramework的介绍、安装和实例运行
- ZendFramework的介绍、安装和实例运行
- ZendFramework-2.1.4 安装实例(win7)
- 基于ZendFramework提炼PHP开发规范
- sprintf()和prinf() 【ZendFramework中PHP】
- Android ant 编译 apk文件名含版本号
- Android常用控件之ViewPager
- 13..[UIApplication sharedApplication].keyWindow,遇到的问题
- 573B - Bear and Blocks
- 字符串转码
- php利用zendframework编程实例
- MFC截屏、绘制并复制到剪切板
- python 基于dns 轮询的业务检测
- LinkedIn架构这十年
- Oracle使用笔记(1)
- java 多线程断点下载
- iOS CGAffineTransform详解
- 文字换行中word-wrap、white-space和word break的区别
- N个tomcat之间实现Session共享