A Practical Tutorial Of Zend Framework(三)

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 
{
    public function 
indexAction()
    {
        
$view = new Zend_View();
        
$view->setScriptPath('/path/to/views');
        echo 
$view->render('example.php');
    }

    public function 
noRouteAction()
    {
        
$this->_redirect('/');
    }
}

?>
Create a file called example.php in the views directory:
<html>
<head>
    <title>This Is an Example</title>
</head>
<body>
    <p>This is an example.</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 
{
    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('/');
    }
}

?>
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.

转载于:https://my.oschina.net/elinac/blog/698020

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值