㈠ 建议
@(未配置ZendFramwork的同学,在我的博客里:[ NetBeans配置zendFramework ],观看如何配置)
@ 到网上下载个 zendFramework的API (建议下载英文文档)。
打开API,点击 Learnint Zend Framework 路径下的
- Zend Framework Quick Start
@ 根据里面的教程走,就能创建一个‘留言板’。
㈡ 用zend命令创建Zend Framwork项目的步骤:
首先,我们需要了解一下, 到底需要执行哪些步骤
① 创建数据库表 | : test数据库下,创建guestbook表 |
② 配置 Bootstrap.php | :"引导文件" |
③ 创建 layout | :"布局" |
④ 配置 applicatin.ini | :"配置文件" :配置数据库、布局...信息 |
⑤ 创建model | :"模型",2部分:1实体bean; 2数据库入口方法(select,insert,update,delete) |
⑥ 创建dbModel | :"表关联模型":指定模型映射哪一个数据表 |
⑦ 创建 mapper | :"模型入口实现" :模型的入口方法在这里实现 |
⑧ 创建 Controller | :"控制器":调用模型方法,获值后传递给视图 |
⑨ 创建 view | :"显示层phtml",PHP的HTML页面 |
⑩ 演示 | |
㈢ 注意事项:
创建 layout、Controller的时候,一定得用 zend命令 创建,不要手动创建。
| | |
■ .zfproject.xml文件在项目里看不到,得到你的工作空间所在的项目下才能看到。里面是关于Controller调用Action的配置 | | |
| |
@ 打开命令窗口 如图:↓
㈣ 创建zendFramework实例过程
① 创建数据库表 ( 在test数据库,创建 guestbook 表) 代码如下↓
use test;
CREATE TABLE guestbook (
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
email VARCHAR(32) NOT NULL DEFAULT 'noemail@test.com',
comment TEXT NULL,
created DATETIME NOT NULL
);
CREATE INDEX id ON guestbook(id);
INSERT INTO guestbook (email, comment, created) VALUES
('ralph.schindler@zend.com','Hello! Hope you enjoy this sample zf application!', now());
INSERT INTO guestbook (email, comment, created) VALUES
('foo@bar.com', 'Baz baz baz, baz baz Baz baz baz - baz baz baz.', now());
② 修改Bootstrap.php (引导文件)
./application/Bootstrap.php 将以下代码替换进去↓
/**
* 添加引导资源的最简单方法是只需用短语_init创建一个受保护的方法开始。在这种情况下,
* 我们想要初始化 doctype,所以我们将我们引导类别内创建_initDoctype()的一种方法:
*/
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initDoctype()
{
//初始化视图
$this->bootstrap('view');
$view = $this->getResource('view');
$view->doctype('XHTML1_STRICT');
}
}
③ 创建layout (布局)
@ 执行zend命令,创建layout。
过滤器: enable config
@ 执行后,自动创建一个layout.ptml,地址如下:
./application/layouts/scripts/layout.ptml 将以下代码替换进去↓
<?php echo $this->doctype() ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Zend Framework Quickstart Application</title>
<?php echo $this->headLink()->appendStylesheet('/css/global.css') ?>
</head>
<body>
<div id="header" style="background-color: #EEEEEE; height: 30px;">
<div id="header-logo" style="float: left">
<b>ZF Quickstart Application</b>
</div>
<div id="header-navigation" style="float: right">
<a href="<?php echo $this->url(
array('controller'=>'guestbook'),
'default',
true) ?>">Guestbook</a>
</div>
</div>
<?php echo $this->layout()->content ?>
</body>
</html>
④ 配置application.ini(配置数据库)
./application/configs/application.ini 将以下代码替换进去↓
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
;布局 ( zend命令创建layout的时候,自动生成这一段信息 )
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
;数据库
resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = ogepyj
resources.db.params.dbname = test
resources.db.params.charset = utf8
;视图
resources.view[] =
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
⑤ 创建模型Guestbook.php(模型:1 实体bean;2 增删改查的入口方法)
@ 在以下路径创建一个Guestbook.php
./application/models/Guestbook.php 将以下代码替换进去↓
class Application_Model_Guestbook
{
protected $_comment;
protected $_created;
protected $_email;
protected $_id;
protected $_mapper;
public function __construct(array $options = null) {
if (is_array($options)) {
$this->setOptions($options);
}
}
public function __set($name, $value){
$method = 'set' . $name;
if (('mapper' == $name) || !method_exists($this, $method)) {
throw new Exception('Invalid guestbook property');
}
$this->$method($value);
}
public function __get($name){
$method = 'get' . $name;
if (('mapper' == $name) || !method_exists($this, $method)) {
throw new Exception('Invalid guestbook property');
}
return $this->$method();
}
public function setOptions(array $options){
$methods = get_class_methods($this);
foreach ($options as $key => $value) {
$method = 'set' . ucfirst($key);
if (in_array($method, $methods)) {
$this->$method($value);
}
}
return $this;
}
public function setComment($text){
$this->_comment = (string) $text;
return $this;
}
public function getComment(){
return $this->_comment;
}
public function setEmail($email){
$this->_email = (string) $email;
return $this;
}
public function getEmail(){
return $this->_email;
}
public function setCreated($ts){
$this->_created = $ts;
return $this;
}
public function getCreated(){
return $this->_created;
}
public function setId($id){
$this->_id = (int) $id;
return $this;
}
public function getId(){
return $this->_id;
}
public function setMapper($mapper){
$this->_mapper = $mapper;
return $this;
}
public function getMapper(){
if (null === $this->_mapper) {
$this->setMapper(new Application_Model_GuestbookMapper());
}
return $this->_mapper;
}
public function save(){
$this->getMapper()->save($this);
}
public function find($id){
$this->getMapper()->find($id, $this);
return $this;
}
public function fetchAll(){
return $this->getMapper()->fetchAll();
}
}
⑥ 创建表关联模型(指定模型映射哪一个数据表)
@ 在以下位置创建 Guestbook.php (由于DbTable文件夹不存在,所以得先创建)
./application/models/DbTable/Guestbook.php 将以下代码替换进去↓
class Application_Model_DbTable_Guestbook extends Zend_Db_Table_Abstract
{
/** Table name */
protected $_name = 'guestbook';
}
⑦ 创建GuestbookMapper.php
(模型入口实现:模型的入口方法(select、insert、update、delete),在这里实现)
./application/models/GuestbookMapper.php 将以下代码替换进去↓
class Application_Model_GuestbookMapper
{
protected $_dbTable;
public function setDbTable($dbTable)
{
if (is_string($dbTable)) {
$dbTable = new $dbTable();
}
if (!$dbTable instanceof Zend_Db_Table_Abstract) {
throw new Exception('Invalid table data gateway provided');
}
$this->_dbTable = $dbTable;
return $this;
}
public function getDbTable(){
if (null === $this->_dbTable) {
$this->setDbTable('Application_Model_DbTable_Guestbook');
}
return $this->_dbTable;
}
public function save(Application_Model_Guestbook $guestbook){
$data = array(
'email' => $guestbook->getEmail(),
'comment' => $guestbook->getComment(),
'created' => date('Y-m-d H:i:s'),
);
if (null === ($id = $guestbook->getId())) {
unset($data['id']);
$this->getDbTable()->insert($data);
} else {
$this->getDbTable()->update($data, array('id = ?' => $id));
}
}
public function find($id, Application_Model_Guestbook $guestbook){
$result = $this->getDbTable()->find($id);
if (0 == count($result)) {
return;
}
$row = $result->current();
$guestbook->setId($row->id)
->setEmail($row->email)
->setComment($row->comment)
->setCreated($row->created);
}
public function fetchAll(){
$resultSet = $this->getDbTable()->fetchAll();
$entries = array();
foreach ($resultSet as $row) {
$entry = new Application_Model_Guestbook();
$entry->setId($row->id)
->setEmail($row->email)
->setComment($row->comment)
->setCreated($row->created)
->setMapper($this);
$entries[] = $entry;
}
return $entries;
}
}
⑧ 创建控制器GuestbookController.php(控制器:调用模型方法,获值后传递给视图)
@ zend命令创建 GuestbookController.php
过滤器 :create controller
参数:Guestbook
执行后,GuestbookController.php的路径如下:
./application/controllers/GuestbookController.php 将以下代码替换进去↓
class GuestbookController extends Zend_Controller_Action
{
public function indexAction()
{
$guestbook = new Application_Model_Guestbook();
$this->view->entries = $guestbook->fetchAll();
}
}
⑨ 创建视图index.phtml(视图:显示数据)
@ 在以下位置创建一个 index.phtml (由于guestbook文件夹不存在,所以得先创建)
./application/views/scripts/guestbook/index.phtml 将以下代码替换进去↓
<!-- application/views/scripts/guestbook/index.phtml -->
<p><a href="<?php echo $this->url(
array(
'controller' => 'guestbook',
'action' => 'sign'
),
'default',
true) ?>">Sign Our Guestbook</a></p>
Guestbook Entries: <br />
<dl>
<?php foreach ($this->entries as $entry): ?>
<dt><?php echo $this->escape($entry->email) ?></dt>
<dd><?php echo $this->escape($entry->comment) ?></dd>
<?php endforeach ?>
</dl>
⑩ 演示
@输入以下地址进行测试:(每个人项目放的工作空间不一样,以至于地址不一样,测试的时候,要仔细对应)
http://localhost/zendFramework/zendtest/public
@ 在右侧有个链接:Guestbook 点击进去后,就是刚才项目所做的效果
@ 项目效果界面↓