Zend Framework体验
时间:2006-04-02
来源:互联网
这回要说的是Zend公司推出的Zend Framework(下面简称ZF),网址是http://framework.zend.com/,目前的版本是Preview 0.1.2,还不清楚正式版释放的计划。
本文最初发表于我的blog http://blog.csdn.net/Aryang/,转载请注明出处。
先说说安装条件,ZF要求基于PHP5,还有需要Apache mod_rewrite的支持,没有考证这个是否是Apache的内置模块,我是通过加参数--enable-rewrite重新编译了Apache。
下一步就是指定rewrite的规则,有很多方法,我用的是.htaccess文件的方法。在web根目录,也就是index.php所在的目录创建文件.htaccess(注意.开头的文件在unix下默认是隐藏的)。文件的内容如下
代码:
RewriteEngine onRewriteOptions Inherit
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?apache_path=$1 [L,QSA]
代码:
Options FollowSymLinksAllowOverride All
当然rewrite的规则有很多种写法,网上还有一种写法是:
代码:
RewriteEngine onRewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
然后就是下载ZF了,我的习惯是把在php.ini里include_path加上ZF/library目录,这样整个网站就可以很方便的访问ZF的库了。
这里要说说rewrite的作用了。ZF彻底封装了URL的格式,基本上看不到.php的影子了。ZF的App的根目录里只有一个index.php, rewrite会把所有的URL请求改写成对index.php的调用。index.php相当于是个Dispatch,里面会创建一个Zend_Controller_Front对象,它作为一个代理把HTTP请求传递给(dispatch)相应的你写好的Controller。
然后看网上的Tutorial,找到个例子,改了改,如下
代码:
<?php//index.php
include 'Zend.php'; //我是已经加入了include_path,没加的写全路径
Zend::loadClass('Zend_Controller_Action');
Zend::loadClass('Zend_Controller_Front');
Zend::loadClass('Zend_InputFilter');
Zend::loadClass('Zend_View');
$controller = Zend_Controller_Front::getInstance();
$controller->setControllerDirectory('./controllers');
/*
//数据库调用,可以先不加
require_once 'Zend/Db.php';
$params = array (
'database' => 'mydb',
'username' => 'myname',
'password' => 'mypass'
);
$db = Zend_Db::factory('Oracle', $params);
Zend::register('db', $db); //全局对象注册,很好的方式
*/
$view = new Zend_View;
$view->setScriptPath('./views');
Zend::register('view', $view);
$controller->dispatch();
?>
代码:
class IndexController extends Zend_Controller_Action{
function indexAction()
{
echo "这是首页";
}
function noRouteAction()
{
echo "您所请求的页面不存在!";
}
}
"http://test.com/book.php?action=edit&book_id=888"
如何实现呢?在bookController类里定义“"editAction"方法,URL调用的写法是"http://test.com/book/edit/book_id/888",格式就是
http://host/controller_name/method_name/param1/value1/param2/value2/...
在editAction方法里可以通过 $this->_getParam("param1") 获得参数,或通过
$this->_getAllParams()获得全部的参数。
代码:
class bookController extends Zend_Controller_Action{
function indexAction()
{
echo "hi, this is my book";
}
function editAction()
{
$book_id = $this->_getParam("book_id");
echo "book id:".$book_id;
}
}
所谓"Framework"都是这样的,它调用你,不是你调用它。
目前感觉严重不爽的就是应用必须是网站的根目录,实际一个网站有多个应用的话,我们一般就是把每个应用放在不同的目录下。行动,在ZF的邮件列表里发现老外对这个也有意见,很多人做了处理。看到了这个网站
http://kpumuk.info/php/zend-framework-router-for-subdirectory-based-site/
里面实现了对App存放子目录的处理,大概就是自己写了个Router,替换掉ZF里的Router。但这个网站提供的代码在ZF 0.1.2里运行有问题,估计他是在0.1.0的时候写的。我做了相应修改:
代码:
<?php/** Zend_Controller_Router_Interface */
require_once 'Zend/Controller/Router/Interface.php';
/** Zend_Controller_Dispatcher_Interface */
require_once 'Zend/Controller/Dispatcher/Interface.php';
/** Zend_Controller_Router_Exception */
require_once 'Zend/Controller/Router/Exception.php';
/** Zend_Controller_Dispatcher_Action */
require_once 'Zend/Controller/Dispatcher/Interface.php';
class Zend_Controller_SubDirectoryRouter implements Zend_Controller_Router_Interface
{
public function route(Zend_Controller_Dispatcher_Interface $dispatcher)
{
// SubDirectoryRouter: what's the path to where we are?
$pathIndex = dirname($_SERVER['SCRIPT_NAME']);
// SubDirectoryRouter: remove $pathIndex from $_SERVER['REQUEST_URI']
$path = str_replace($pathIndex, '', $_SERVER['REQUEST_URI']);
if (strstr($path, '?')) {
$path = substr($path, 0, strpos($path, '?'));
}
$path = explode('/', trim($path, '/'));
/**
* The controller is always the first piece of the URI, and
* the action is always the second:
*
* http://zend.com/controller-name/action-name/
*/
$controller = $path[0];
$action = isset($path[1]) ? $path[1] : null;
/**
* If no controller has been set, IndexController::index()
* will be used.
*/
if (!strlen($controller)) {
$controller = 'index';
$action = 'index';
}
/**
* Any optional parameters after the action are stored in
* an array of key/value pairs:
*
* http://www.zend.com/controller-name/action-name/param-1/3/param-2/7
*
* $params = array(2) {
* ["param-1"]=> string(1) "3"
* ["param-2"]=> string(1) "7"
* }
*/
$params = array();
for ($i=2; $i<sizeof($path); $i=$i+2) {
$params[$path[$i]] = isset($path[$i+1]) ? $path[$i+1] : null;
}
$actionObj = new Zend_Controller_Dispatcher_Token($controller, $action, $params);
if (!$dispatcher->isDispatchable($actionObj)) {
/**
* @todo error handling for 404's
*/
throw new Zend_Controller_Router_Exception('Request could not be mapped to a route.');
} else {
return $actionObj;
}
}
}
?>
在
$controller->dispatch();
之前加入如下:
Zend::loadClass('Zend_Controller_SubDirectoryRouter');
$router = new Zend_Controller_SubDirectoryRouter();
$controller->setRouter($router);
这样就可以把我们的应用置于网站某子目录下了。
ZF里有很多内容,这是我刚接触了解到的,如有新的体验,再和诸位一起探讨(wdy)。
作者: Aryang 发布时间: 2006-04-01
作者: Per 发布时间: 2006-04-02
作者: seraph 发布时间: 2006-04-03
作者: 刀客羽朋 发布时间: 2006-04-03
引用:
原帖由 Per 于 2006-4-2 08:47 发表好,我们对这个,搞个专题。
:)
作者: Bantu 发布时间: 2006-04-05
我下了 ZF 0.1.2 版本,也把路径加到 include_path 中了. 可是一直不好使, 老是说找不到文件.郁闷....
作者: dennis 发布时间: 2006-04-08
作者: levi 发布时间: 2006-04-26
自己花点心思琢磨一下主要功能,然后模仿之
何必大而全呢?结果大家都一样。
一点个性都没有。
作者: jefsun 发布时间: 2006-08-25
顶上去
作者: fengyun 发布时间: 2006-09-02
好东西,支持一下
作者: skendy 发布时间: 2007-04-14
作者: leehui1983 发布时间: 2007-04-16
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28