A Practical Tutorial Of Zend Framework(三)

    技术2022-05-11  5


    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: <?phpZend::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: <?phpZend::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: <?phpZend::register('view'$view);?> To retrieve it, use the registry() method: <?php$view Zend::registry('view');?> From this point forward, this tutorial uses the registry.