当前位置: 首页 > 图文教程 > 网络编程 > PHP > Zend Framework 入门——快速上手

PHP
PHP 手机归属地查询 api
php 自写函数代码 获取关键字 去超链接
检查url链接是否已经有参数的php代码 添加 ? 或 &
PHP生成网页快照 不用COM不用扩展.
一步一步学习PHP(1) php开发环境配置
一步一步学习PHP(2):PHP类型
一步一步学习PHP(3) php 函数
一步一步学习PHP(4) php 函数 补充2
提高PHP编程效率 引入缓存机制提升性能
php 数组的合并、拆分、区别取值函数集
PHP采集相关教程之一 CURL函数库
IP138 IP地址查询小偷实现代码
php 生成静态页面的办法与实现代码详细版
一步一步学习PHP(5) 类和对象
一步一步学习PHP(6) 面向对象
Apache环境下PHP利用HTTP缓存协议原理解析及应用分析
PHP 截取字符串函数整理(支持gb2312和utf-8)
php foreach 使用&(与运算符)引用赋值要注意的问题
PHP IPV6正则表达式验证代码
用PHP ob_start()控制浏览器cache、生成html实现代码

PHP 中的 Zend Framework 入门——快速上手


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 58 ::
收藏到网摘: n/a

1. 安装

从 Zend Framework 的网页上下载最新版本。解压后,把整个目录拷贝到一个理想的地方,比如:/php/library/Zend。

打开 php.ini 文件,确认包含 Zend 目录的路径在 include_path 里定义了。以上面的配置为例,php.ini 中应有类似下面的条目:

include_path = ".:/php/library"

注意:Windows 下的写法略有不同,应该类似于 include_path = ".;C:\php\library"

初始的安装就这么简单。Zend Framework 的一些组件会用到 php 的一些附加模块。具体的要求请参考这里。

2. 项目的目录结构

如果你的项目不包含多个模块,可以用下面的目录结构:

application/
controllers/
IndexController.php
models/
views/
scripts/
index/
index.phtml
helpers/
filters/
html/
.htaccess
index.php

如果你的项目要包含多个模块(比如:博客,社区,等等),那么建议使用模块化的目录结构。

3. 网页的根目录

网页的根目录应指向上述目录结构中的 html 文件夹。

4. 重写规则

编辑 html/.htaccess 文件,加入下面两行:

RewriteEngine onRewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
注意:上述是针对 apache 的配置。如果是其他的服务器,请参考这里。

5. 引导程序

编辑 html/index.php 文件,敲入下面代码:

上面代码的作用是实例化前端控制器(Front Controller)并运行它。

6. 默认的动作控制器(Action Controller)

Zend Framework 的默认路由规则是 http://域名/控制器名/动作(方法)名。例如:

http://example.com/user/show 会被解析到名为 User 的控制器以及该控制器中定义的 show 方法。如果该方法没有定义,则默认转到 index 方法。

注意:在代码中,控制器名的后面要加上 Controller,而动作名的后面要加上 Action。

编辑 application/controllers/IndexController.php 文件,输入:

<?php

/** Zend_Controller_Action */

require_once 'Zend/Controller/Action.php';

class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
    }
}

7. 视图(页面)脚本

编辑 application/views/scripts/index/index.phtml,输入:

<!DOCTYPE htmlPUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head> 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

<title>My first Zend Framework App</title>

</head>

<body>   

<h1>Hello, World!</h1>

</body>

</html>

8. 错误控制器

默认情况下,Zend Framework 的错误处理插件是被注册的。它需要一个错误控制器来处理错误。缺省的错误控制处理被假定为 ErrorController 以及其中定义的 errorAction。

编辑 application/controllers/ErrorController.php,输入:

<?php
/** Zend_Controller_Action */
require_once 'Zend/Controller/Action.php';

class ErrorController extends Zend_Controller_Action
{
    public function errorAction()
    {
    }
}

下面是对应的视图脚本。编辑 application/views/scripts/error/error.phtml,输入:

<!DOCTYPE htmlPUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head> 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

<title>Error</title>

</head>

<body>   

<h1>An error occurred</h1>   

<p>An error occurred; please try again later.</p>

</body>

</html>

9. 运行

好,现在运行网站。在浏览器中键入下面三个地址,得到的结果应该是一样的——就是最最常见的“Hello, World!“。

http://域名
http://域名/index
http://域名/index/index
如果是这样,那么恭喜你!

相关文章

Zend Framework 入门——快速上手

Zend Framework 入门——多国语言支持

Zend Framework 入门——错误处理

Zend Framework 入门——页面布局