同时还有中文Zend Php Framework
Zend Framework 中文版手册在线浏览
The Zend Framework has been unveiled! Although it is still in the early stages of development, this tutorial highlights some of the best of what's available now and guides you through the process of building a simple application.
Zend has chosen to release the framework and involve the community early. In the same spirit, this tutorial is written to showcase the framework as it exists today. Because this tutorial is published online, I'll update it as the framework evolves, so that it remains relevant as long as possible.
Requirements
The Zend Framework requires PHP 5. In order to take full advantage of the code presented in this tutorial, you also need the Apache web server, because the sample application (a news management system) uses
mod_rewrite
.
The code from this tutorial is available for free download, so you can try it out for yourself. You can download it from the Brain Bulb web site at http://brainbulb.com/zend-framework-tutorial.tar.gz.
Downloading the Framework
Before you can get started with this tutorial, you need to download the preview release of the framework. You can do this manually by visiting http://framework.zend.com/download with a browser and choosing the
tar.gz
or
zip
download, or you can use the command line:
$wgethttp://framework.zend.com/download/tgz
$tar-xvzfZendFramework-0.1.2.tar.gz
Note:
Zend has plans to offer its own PEAR channel to help simplify the download process.
Once you have the preview release, locate the
library
directory and place it somewhere convenient. In this tutorial, I rename
library
to
lib
to provide a clean and simple directory structure:
app/
views/
controllers/
www/
.htaccess
index.php
lib/
The
www
directory is the document root,
controllers
and
views
are empty directories you'll use later, and the
lib
directory is from the preview release download.
Getting Started
The first component I want to show you is
Zend_Controller
. In many ways, it provides the foundation of the application you're developing, and it's also part of what makes the Zend Framework more than just a collection of components. Before you can use it, however, you need to direct all incoming requests to a single PHP script. This tutorial uses
mod_rewrite
for this purpose.
Using
mod_rewrite
is an art in itself, but luckily, this particular task is very simple. If you're unfamiliar with
mod_rewrite
or configuring Apache in general, create a
.htaccess
file in the document root and add the following directives:
RewriteEngineon
RewriteRule!\.(js|ico|gif|jpg|png|css)$index.php
Note:
One of the TODO items for
Zend_Controller
is to remove the
mod_rewrite
dependency. In order to provide an example that works with the preview release, this tutorial uses
mod_rewrite
.
If you add these directives to
httpd.conf
directly, you must restart the web server. Otherwise, if you use a
.htaccess
file, you should be good to go. You can quickly test this by placing some identifiable text in
index.php
and making a request for an arbitrary path such as
/foo/bar
. For example, if your host is
example.org
, request the URL http://example.org/foo/bar.
You also want
include_path
to include the path to the framework library. You can do this in
php.ini
, or you can just put the following directive in your
.htaccess
file:
php_value include_path "/path/to/lib"
Zend
The
Zend
class contains a collection of static methods that are universally useful. This is the only class you must include manually:
<?php
include
'Zend.php'
;
?>
Once you've included
Zend.php
, you have access to all of the methods from the
Zend
class. Loading other classes is simplified with the
loadClass()
method. For example, to load the
Zend_Controller_Front
class:
<?php
include
'Zend.php'
;
Zend
::
loadClass
(
'Zend_Controller_Front'
);
?>
The
loadclass()
method is
include_path
aware, and it also understands the organization and directory structure of the framework. I use it to load all other classes.
Zend_Controller
Using the controller is pretty intuitive. In fact, I'm writing this tutorial without the luxury of documentation!
Note:
Documentation is now available at http://framework.zend.com/manual/zend.controller.html.
I begin with
Zend_Controller_Front
, a front controller. In order to begin understanding how it works, place the following code in your
index.php
file:
<?php
include
'Zend.php'
;
Zend
::
loadClass
(
'Zend_Controller_Front'
);
$controller
=
Zend_Controller_Front
::
getInstance
();
$controller
->
setControllerDirectory
(
'/path/to/controllers'
);
$controller
->
dispatch
();
?>
If you prefer object chaining, this can instead be written as:
<?php
include
'Zend.php'
;
Zend
::
loadClass
(
'Zend_Controller_Front'
);
$controller
=
Zend_Controller_Front
::
getInstance
()
->
setControllerDirectory
(
'/path/to/controllers'
)
->
dispatch
();
?>
Now when you make a request for
/foo/bar
, you get an error. That's good! It lets you know something is happening. The major complaint is that
IndexController.php
is not found.
Before you create this file, it's helpful to understand how the framework expects you to organize things. The framework breaks a request down into parts, and in the case of a request for
/foo/bar
,
foo
is the controller, and
bar
is the action. The default value for each is
index
.
When
foo
is the controller, the framework looks for a file called
FooController.php
in the
controllers
directory. Because this does not exist, the framework falls back to
IndexController.php
. Not finding either, it reports the error.
To continue, create
IndexController.php
in the
controllers
directory (which you set with
setControllerDirectory()
):
<?php
Zend
::
loadClass
(
'Zend_Controller_Action'
);
class
IndexController
extends
Zend_Controller_Action
{
publicfunction
indexAction
()
{
echo
'IndexController::indexAction()'
;
}
}
?>
The
IndexController
class handles requests for which the controller is
index
or for which the indicated controller does not exist, as just explained. The
indexAction()
method handles requests for which the action is
index
. Remember that
index
is the default value for both the controller and the action. If you try a request for
/
,
/index
, or
/index/index
, the
indexAction()
method is executed. (Trailing slashes do not alter this behavior.) A request for any other resource is going to result in an error.
There is another useful method to add to
IndexController
before continuing. The
noRouteAction()
method is called whenever a request is made for a controller that doesn't exist. For example, a request for
/foo/bar
executes
noRouteAction()
if
FooController.php
does not exist. However, a request for
/index/foo
still results in an error, because
foo
is the action, not the controller.
Add
noRouteAction()
to
IndexController.php
:
<?php
Zend
::
loadClass
(
'Zend_Controller_Action'
);
class
IndexController
extends
Zend_Controller_Action
{
publicfunction
indexAction
()
{
echo
'IndexController::indexAction()'
;
}
publicfunction
noRouteAction
()
{
$this
->
_redirect
(
'/'
);
}
}
?>
This example uses
$this->_redirect('/')
to illustrate a possible action to take in
noRouteAction()
. This causes requests for nonexistent controllers to be redirected to the root document (front page).
Now create
FooController.php
:
<?php
Zend
::
loadClass
(
'Zend_Controller_Action'
);
class
FooController
extends
Zend_Controller_Action
{
publicfunction
indexAction
()
{
echo
'FooController::indexAction()'
;
}
publicfunction
barAction
()
{
echo
'FooController::barAction()'
;
}
}
?>
If you again request
/foo/bar
, you should see that
barAction()
is being executed, because
bar
is the action. Not only can you already support friendly URLs, but you can also do so in a very organized way with just a few lines of code. Cool!
You can also create a
__call()
method to handle requests for undefined actions such as
/foo/baz
:
<?php
Zend
::
loadClass
(
'Zend_Controller_Action'
);
class
FooController
extends
Zend_Controller_Action
{
publicfunction
indexAction
()
{
echo
'FooController::indexAction()'
;
}
publicfunction
barAction
()
{
echo
'FooController::barAction()'
;
}
publicfunction
__call
(
$action
,
$arguments
)
{
echo
'FooController:__call()'
;
}
}
?>
Now that you can elegantly handle incoming requests with just a few lines of code, you are ready to continue.
Zend_View
Zend_View
is a class that helps you organize your view logic. It is template-system agnostic, and for the sake of simplicity, I don't use a template system in this tutorial. You're free to use one if you prefer.
Keep in mind that all incoming requests are now handled by the front controller. Therefore, the framework of the application already exists, and further additions must conform to it. In order to demonstrate a very basic use of
Zend_View
, change the code in
IndexController.php
as follows:
<?php
Zend
::
loadClass
(
'Zend_Controller_Action'
);
Zend
::
loadClass
(
'Zend_View'
);
class
IndexController
extends
Zend_Controller_Action
{
publicfunction
indexAction
()
{
$view
=new
Zend_View
();
$view
->
setScriptPath
(
'/path/to/views'
);
echo
$view
->
render
(
'example.php'
);
}
publicfunction
noRouteAction
()
{
$this
->
_redirect
(
'/'
);
}
}
?>
Create a file called
example.php
in the
views
directory:
<html>
<head>
<title>ThisIsanExample</title>
</head>
<body>
<p>Thisisanexample.</p>
</body>
</html>
Now, when you request your web site's root resource, you should see the contents of
example.php
. This isn't very useful yet, but keep in mind that you're working toward a very structured and organized way to develop web applications.
In order to make the use of
Zend_View
a bit clearer, modify your template (
example.php
) to include some data:
<html>
<head>
<title>
<?php
echo
$this
->
escape
(
$this
->
title
);
?>
</title>
</head>
<body>
<?php
echo
$this
->
escape
(
$this
->
body
);
?>
</body>
</html>
Two additional features have been added. The
$this->escape()
method must be used on all output. Even if you create the output yourself, which is going to be the case in this example, escaping all output is a very good habit that can help you prevent cross-site scripting (XSS) vulnerabilities by default.
The
$this->title
and
$this->body
properties exist to demonstrate dynamic data. These should be defined in the controller, so modify
IndexController.php
to assign them:
<?php
Zend
::
loadClass
(
'Zend_Controller_Action'
);
Zend
::
loadClass
(
'Zend_View'
);
class
IndexController
extends
Zend_Controller_Action
{
publicfunction
indexAction
()
{
$view
=new
Zend_View
();
$view
->
setScriptPath
(
'/path/to/views'
);
$view
->
title
=
'DynamicTitle'
;
$view
->
body
=
'Thisisadynamicbody.'
;
echo
$view
->
render
(
'example.php'
);
}
publicfunction
noRouteAction
()
{
$this
->
_redirect
(
'/'
);
}
}
?>
Now you should see these values being used by the template when you again visit your web site's root directory. The reason you use
$this
in the template is that it is executed within the scope of the
Zend_View
instance.
Keep in mind that
example.php
is just an ordinary PHP script, so you can do anything you want. Just try to be disciplined enough to limit your use of templates to only what is required to display data. Your controller (or a module to which the controller dispatches) should handle all of your business logic.
I want to make one final note about
Zend_View
before continuing. Instantiating the
$view
object within each method of the controller requires a lot of extra typing, and our primary goal is to make it easier to quickly develop web applications. It's also a hassle to call
setScriptPath()
in each case if all of the templates reside in a single directory.
Luckily, the
Zend
class includes a registry that helps you eliminate this overhead. You can store your
$view
object in the registry by using the
register()
method:
<?php
Zend
::
register
(
'view'
,
$view
);
?>
To retrieve it, use the
registry()
method:
<?php
$view
=
Zend
::
registry
(
'view'
);
?>
From this point forward, this tutorial uses the registry.
Zend_InputFilter
The last component this tutorial covers is
Zend_InputFilter
. This class provides a simple but rigid approach to input filtering. You instantiate it by passing an array of data to be filtered:
<?php
$filterPost
=new
Zend_InputFilter
(
$_POST
);
?>
This sets the array (
$_POST
) to
NULL
, so direct access is no longer possible.
Zend_InputFilter
instead provides a small, focused collection of methods that filter data according to specific criteria. For example, if you want the alphabetic characters of
$_POST['name'
], you can use the
getAlpha()
method:
<?php
/*$_POST['name']='John123Doe';*/
$filterPost
=new
Zend_InputFilter
(
$_POST
);
/*$_POST=NULL;*/