Chinaunix首页 | 论坛 | 博客
  • 博客访问: 423778
  • 博文数量: 117
  • 博客积分: 5235
  • 博客等级: 大校
  • 技术积分: 1775
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-12 15:51
文章分类

全部博文(117)

文章存档

2012年(9)

2011年(2)

2010年(21)

2009年(13)

2008年(72)

我的朋友

分类:

2009-05-26 17:36:55

背景:
    前些天刚弄了下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. 测试
地址: 你将看到一个蓝色界面,表示成功
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="" 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>&nbsp;</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. 目前我的截图

阅读(1546) | 评论(0) | 转发(0) |
0

上一篇:PHP xml

下一篇:继承

给主人留下些什么吧!~~