Zend Framework开发入门经典教程(2)

就如刚才说明的,IndexController类处理来自index controller或controller不存在的请求。indexAction()方法处理action为index的访问。要记住的是index是controller和action的默认值。如果你访问/,/index或/index/index,indexAction()方法就会被执行。 (最后面的斜杠并不会改变这个行为。) 而访问其他任何资源只会导致出错。

在继续做之前,还要在IndexController加上另外一个有用的类方法。不管什么时候访问一个不存在的控制器,都要调用noRouteAction()类方法。例如,在FooController.php不存在的条件下,访问/foo/bar就会执行noRouteAction()。但是访问/index/foo仍会出错,因为foo是action,而不是controller.

将noRouteAction()添加到IndexController.php:

<?php Zend::loadClass('Zend_Controller_Action'); class IndexController extends Zend_Controller_Action { public function indexAction() { echo 'IndexController::indexAction()'; } public function noRouteAction() { $this->_redirect('https://www.jb51.net/'); } } ?>

例子中使用$this->_redirect('https://www.jb51.net/')来描述执行noRouteAction()时,可能发生的行为。这会将对不存在controllers的访问重定向到根文档(首页)。

现在创建FooController.php:

<?php Zend::loadClass('Zend_Controller_Action'); class FooController extends Zend_Controller_Action { public function indexAction() { echo 'FooController::indexAction()'; } public function barAction() { echo 'FooController::barAction()'; } } ?>

如果你再次访问/foo/bar,你会发现执行了barAction(),因为bar是action。现在你不只支持了友好的URL,还可以只用几行代码就做得这么有条理。酷吧!
你也可以创建一个__call()类方法来处理像/foo/baz这样未定义的action。

<?php Zend::loadClass('Zend_Controller_Action'); class FooController extends Zend_Controller_Action { public function indexAction() { echo 'FooController::indexAction()'; } public function barAction() { echo 'FooController::barAction()'; } public function __call($action, $arguments) { echo 'FooController:__call()'; } } ?>

现在你只要几行代码就可以很好地处理用户的访问了,准备好继续。

Zend_View

Zend_View是一个用来帮助你组织好你的view逻辑的类。这对于模板-系统是不可知的,为了简单起见,本教程不使用模板。如果你喜欢的话,不妨用一下。

记住,现在所有的访问都是由front controller进行处理。因此应用框架已经存在了,另外也必须遵守它。为了展示Zend_View的一个基本应用,将IndexController.php修改如下:

<?php Zend::loadClass('Zend_Controller_Action'); Zend::loadClass('Zend_View'); class IndexController extends Zend_Controller_Action { public function indexAction() { $view = new Zend_View(); $view->setScriptPath('/path/to/views'); echo $view->render('example.php'); } public function noRouteAction() { $this->_redirect('https://www.jb51.net/'); } } ?>

在views目录创建example.php文件:

<html> <head> <title>This Is an Example</title> </head> <body> <p>This is an example.</p> </body> </html>

现在,如果你访问自己网站的根资源,你会看到example.php的内容。这仍没什么用,但你要清楚你要在以一种结构和组织非常清楚的方式在开发网络应用。

为了让Zend_View的应用更清楚一点,,修改你的模板(example.php)包含以下内容:

<html> <head> <title><?php echo $this->escape($this->title); ?></title> </head> <body> <?php echo $this->escape($this->body); ?> </body> </html>

现在已经添加了两个功能。$this->escape()类方法用于所有的输出。即使你自己创建输出,就像这个例子一样。避开所有输出也是一个很好的习惯,它可以在默认情况下帮助你防止跨站脚本攻击(XSS)。

$this->title和$this->body属性用来展示动态数据。这些也可以在controller中定义,所以我们修改IndexController.php以指定它们:

<?php Zend::loadClass('Zend_Controller_Action'); Zend::loadClass('Zend_View'); class IndexController extends Zend_Controller_Action { public function indexAction() { $view = new Zend_View(); $view->setScriptPath('/path/to/views'); $view->title = 'Dynamic Title'; $view->body = 'This is a dynamic body.'; echo $view->render('example.php'); } public function noRouteAction() { $this->_redirect('https://www.jb51.net/'); } } ?>

现在你再次访问根目录,应该就可以看到模板所使用的这些值了。因为你在模板中使用的$this就是在Zend_View范围内所执行的实例。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/1ae32a9c868d85e46ce827f950b95c9f.html