免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1646 | 回复: 0
打印 上一主题 下一主题

zend framework 1.8.1 start [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-26 17:36 |只看该作者 |倒序浏览
背景:
    前些天刚弄了下1.7版本的ZF,结果新版本又出来了,变化其实挺大的,特别是在安装过程中.于是更新一下.

正文:

1.8 Zend安装
与之前版本相比,新版将安装过程部分脚本化,主要是创建骨架过程。并且增加了一些配置文件使框架更加的可配置。
1. 下载
下载,并解压,将bin, library拷贝到想要的位置,一般是c:\Programe File\dirName下
2. 将bin的路径加到系统环境变量PATH中,执行zf show version测试,如果php.exe不能被发现,则查找其路径同样也加到PATH中即可
3. 到web server的root目录下,执行
    zf create project zf-tutorial
创建zf-tutorial骨架,当然目录名任意
4. 拷贝library/Zend到zf-tutorial/library下
5. 设置apache
    AllowOverride None -> AllowOverride All
6. 测试
地址:http://localhost/zf-tutorial/public, 你将看到一个蓝色界面,表示成功
7.一些文件简介
public/.htaccess
public/index.php
application/configs/application.ini
8.添加一个action
切换目录到zf-tutorial/
zf create action add index
9. 数据库
9.1 configuration
file: application/configs/application.ini
like this in [production]:
resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = mysql
resources.db.params.dbname = zf-tutorial
9.2 如果用PDO_MYSQL要在php.ini中打开相应扩展
extension=php_pdo.dll
extension=php_pdo_mysql.dll   //这一点很重要,我就在这点上吃了苦头
9.3 准备数据库
表albums:
CREATE TABLE albums (
id int(11) NOT NULL auto_increment,
artist varchar(100) NOT NULL,
title varchar(100) NOT NULL,
PRIMARY KEY (id)
);
插入数据:
INSERT INTO albums (artist, title)
VALUES
('Bob Dylan', 'Together Through Life'),
('Various Artists', 'Now That\'s what I Call 72'),
('Lady Gaga', 'The Fame'),
('Lily Allen', 'It\'s Not Me, It\'s You'),
('Kings of Leon', 'Only By The Night
9.4 Model class
zf-tutorial/application/models/DbTable/Albums.php
class Model_DbTable_Albums extends Zend_Db_Table
{
  protected $_name = 'albums';
  public function getAlbum($id)
  {
    $id = (int)$id;
    $row = $this->fetchRow('id = ' . $id);
    if (!$row) {
    throw new Exception("Count not find row $id");
    }
    return $row->toArray();
  }
  public function addAlbum($artist, $title)
  {
    $data = array(
    'artist' => $artist,
    'title' => $title,
    );
    $this->insert($data);
  }
  function updateAlbum($id, $artist, $title)
  {
    $data = array(
    'artist' => $artist,
    'title' => $title,
    );
    $this->update($data, 'id = '. (int)$id);
  }
  function deleteAlbum($id)
  {
    $this->delete('id =' . (int)$id);
  }
}
//In fact, now it is ok. You can call model class in controllers.
10. Layouts and views
10.1 views
views is the bridge between controllers and *.phtml
$view = new Zend_View();  //create a view instance
$vies相当于html页面在controller中的接口,controller通过设置view的一些属性来决定html的模样,如$this->view->title, $this->view->headTitle
10.2 layout
10.2.1 layout是.phtml的模板
file: application/configs/application.ini
resources.layout.layoutpath = APPLICATION_PATH "/layouts"
//配置layouts的路径
10.2.2
zf-tutorial/application/layouts/layout.phtml
?php echo $this->doctype('XHTML1_TRANSITIONAL'); ?>
html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
head>
?php echo $this->headMeta(); ?>
?php echo $this->headTitle(); ?>
/head>
body>
div id="content">
h1>?php echo $this->escape($this->title); ?>/h1>
?php echo $this->layout()->content; //模板以外的内容?>
/div>
/body>
/html>
10.3 初始化view and layout
application/Bootstrap.php
...
function _initViewHelpers()
{
  $this->bootstrap('layout');
  $layout = $this->getResource('layout');
  $view = $layout->getView();
  $view->doctype('XHTML1_STRICT');
  $view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
  $view->headTitle()->setSeparator(' - ');
  $view->headTitle('Zend Framework Tutorial');
}
...
11. style, css
11.1 保证路径正确
zf-tutorial/application/views/helpers/BaseUrl.php
?php
class Zend_View_Helper_BaseUrl
{
  function baseUrl()
  {
    $fc = Zend_Controller_Front::getInstance();
    return $fc->getBaseUrl();
  }
}
//获取basedir
11.2 添加入模板
zf-tutorial/application/layouts/layout.phtml
...
head>
?php echo $this->HeadMeta(); ?>
?php echo $this->headTitle(); ?>
?php echo $this->headLink()->prependStylesheet($this->baseUrl().'/css/site.css'); ?>
/head>
...
11.3 编辑css
zf-tutorial/public/css/site.css
body,html {
margin: 0 5px;
font-family: Verdana,sans-serif;
}
h1 {
font-size: 1.4em;
color: #008000;
}
a {
color: #008000;
}
/* Table */
th {
text-align: left;
}
td, th {
padding-right: 5px;
}
/* style form */
form dt {
width: 100px;
display: block;
float: left;
clear: left;
}
form dd {
margin-left: 0;
float: left;
}
form #submitbutton {
margin-left: 100px;
}
//it's all right
12. All is useful
12.1 controller call
zf-tutorial/application/controllers/IndexController.php
...
function indexAction()
{
$this->view->title = "My Albums";
$this->view->headTitle($this->view->title, 'PREPEND');
$albums = new Model_DbTable_Albums(); //实例化
$this->view->albums = $albums->fetchAll(); //获取数据,fetchAll是zf的内置函数
}
...
12.2 show
zf-tutorial/application/views/scripts/index/index.phtml
p>a href="url(array('controller'=>'index',
'action'=>'add'));?>">Add new album/a>/p>
table>
tr>
th>Title/th>
th>Artist/th>
th> /th>
/tr>
?php foreach($this->albums as $album) : ?>
tr>
td>?php echo $this->escape($album->title);?>/td>
td>?php echo $this->escape($album->artist);?>/td>
td>
a href="url(array('controller'=>'index',
'action'=>'edit', 'id'=>$album->id));?>">Edit/a>
a href="url(array('controller'=>'index',
'action'=>'delete', 'id'=>$album->id));?>">Delete/a>
/td>
/tr>
?php endforeach; ?>
/table>
13. 目前我的截图



本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/48467/showart_1945580.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP