您的位置:首页 > 编程语言 > PHP开发

Zend_Controller Quick Start

2017-02-15 16:44 246 查看

Zend_Controller Quick Start

P.S:学习Zend1.12,阅读文档时翻译,关键词语并没有翻译,这样比较好理解(2017.02.15)

Zend_Controller Quick Start
介绍

Quick Start
Create the Filesystem Layout

Set the Document Root

Create the Rewrite Rules

创建Bootstrap File引导程序文件

创建默认的Action controller

创建View Script

创建Error Controller

浏览你的网站

介绍

Zend_controller 是Zend框架MVC系统的核心。MVC代表Model-View-Controller,是一种设计模式,目的是从视图逻辑(display logic) 中分离出应用逻辑(application logic)。

Zend_Controller_Front实现的是Front Controller模式(参见P of EAA),所有的请求都会被front controller截获,并被分派(dispatch)到对应URL的Action Controller。

Zend_Controller拥有可扩展的设计理念,通过子类继承现有类,来实现基于the controller family of classes的多种接口(interfaces)和抽象类(abstract classes),或者通过写plugins or action helpers来增加系统的功能性。

Quick Start

如果你需要更多深层的信息,看接下来的sections。如果你只是想快速建立并运行,请继续读。

Create the Filesystem Layout

第一步是创建你的文件系统布局(file system layout)。典型的布局如下

application/
controllers/
IndexController.php
models/
views/
scripts/
index/
index.phtml
helpers/
filters/
public/(这里名字可设置,源文档是http)
.htaccess
index.php


Set the Document Root

在你的web server中,设置document root 为public目录。。。

Create the Rewrite Rules

修改public/.htaccess 文件如下(默认的.htaccess写法,没有它就不能正常访问)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]


Note:Learn about mod_rewrite

上述的rewrite规则允许访问vhost下所有文件。如果有哪些文件你不希望这样做,你可能想要更严格的rules。去Apache官网学习更多关于mod_write

如果使用IIS7.0,使用以下rewrite配置:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^.*$" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}"
matchType="IsFile" pattern=""
ignoreCase="false" />
<add input="{REQUEST_FILENAME}"
matchType="IsDirectory"
pattern="" ignoreCase="false" />
</conditions>
<action type="None" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="^.*$" />
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>


以上的Rules将会按照要求去路由请求(route requests)存在的资源(存在的符号链接(symlinks)、非空文件、非空文件夹),和一些其他的通往front controller 的 requests 。

Note:以上的rewrite rules是Apache的;如果是其他web server的例子,参见router documentation

创建Bootstrap File(引导程序文件)

Bootstrap文件是route所有request的页面——public/index.php。你可以选择用编辑器打开public/index.php并添加如下:

Zend_Controller_Front::run('/path/to/app/controllers');


这将实例化和调用(dispatch)那些route request到action controller的front controller。

创建默认的Action controller

在讨论Action controller之前,你应该先理解request是如何在zend框架中route的。默认情况下URL path的第一段是通向controller的,第二段通向action。例如,给出URL http://framework.zend.com/roadmap/components,path就是roadmap/components,会导向controller roadmap和action组件。如果没有提供action,默认提供index为action,如果没有提供controller,提供index为默认controller(遵守Apache规定自动导向DirectoryIndex)。

Zend_Controller的dispatcher将会把controller value 和maps带到一个class中。默认情况下,这会把controller标题封装(Title-case),尾部跟上一个Controller。因此,刚才的例子中,我们的controller会导向类RoadmapController

同样的,action value 会导向controller类中的一个method。默认情况下,这个value是小写的,还会在后面跟上Action。因此上个例子中,这个action组件变为componentsAction(),最终的method成为RoadmapController::componentsAction()

So,接下来,现在让我们创建一个默认action controller 和action method。鉴于之前的Note,默认的controller和action都称为index。打开文件
application/controllers/IndexController.php
,并添加如下:

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


默认情况下,ViewRenderer(视图渲染器) action helper 是可用的。意思就是说简单定义一个action method和对应的view script,你将立刻得到提供的content。默认情况下(怎么老是这句)Zend_View是在MVC中作为视图层的。这个ViewRenderer做了一些magic的事,使用了controller name(e.g.,index)和现在的action name(e.g.,index)去决定使用什么模板(template)。默认情况下,模板(template)在.phtml扩展名的结尾,也就是说在上面的例子中,模板(template)
index/index.phtml
将被渲染(renderered)。另外,ViewRenderer自动假设目录views/为同级controller目录的基础视图目录,实际的view脚本存放在
views/scripts/
子目录。因此,我们可以在
application/views/scripts/index/index.phtml
里找到我们的模板。

创建View Script

在之前提到的章节(quick start)里,view脚本都放在
application/views/scripts/
;对应默认controller和action的view脚本在
application/views/scripts/index/index.phtml
。创建并写点HTML:

<!DOCTYPE html
PUBLIC "-//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>


就是个hello world。

创建Error Controller

默认情况下,error handler plugin(错误处理程序组件)是已经注册好了的。这个组件是个用来处理error的controller。默认情况下,它假设ErrorController是一个默认模块并带有如下errorAction()方法:

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


按照已经假定好的目录布局,这个文件应该放在
application/controllers/ErrorController.php
。你将需要创建个view视图在
application/views/scripts/error/error.phtml
;样例内容可能是这样:

<!DOCTYPE html
PUBLIC "-//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>


浏览你的网站

围绕着你第一个建立的controller和view,你可以启动你的浏览器去浏览你的网站。假设example.com是你的域名,如下所有的URLs都可以访问:

http://example.com/

http://example.com/index

http://example.com/index/index

现在,你可以去创建更多的controllers和action方法了。Congratulations!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  zend 文档