加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

Zend Framework教程之视图组件Zend_View用法详解

发布时间:2020-05-23 15:58:06 所属栏目:PHP 来源:互联网
导读:这篇文章主要介绍了Zend Framework教程之视图组件Zend_View用法,较为详细的分析了试图组件Zend_View原理并结合实例形式分析了Zend_View的使用技巧,需要的朋友可以参考下

本文实例讲述了Zend Framework教程之视图组件Zend_View用法。分享给大家供大家参考,具体如下:

Zend_View是Zend Framework的视图组件,MVC中的视图层。 Zend_View也是应用的直接对用户展示的页面。这里介绍一下Zend_View的实现类,以及如何和Controller结合在一起的。

View的实现

Zend_View的实现主要是通过如下目录的类实现:

root@coder-671T-M:/library/Zend# tree | grep View.php │ └── View/ ├── View.php

root@coder-671T-M:/library/Zend/View# tree . ├── Abstract.php ├── Exception.php ├── Helper │ ├── Abstract.php │ ├── Action.php │ ├── BaseUrl.php │ ├── Currency.php │ ├── Cycle.php │ ├── DeclareVars.php │ ├── Doctype.php │ ├── Fieldset.php │ ├── FormButton.php │ ├── FormCheckbox.php │ ├── FormElement.php │ ├── FormErrors.php │ ├── FormFile.php │ ├── FormHidden.php │ ├── FormImage.php │ ├── FormLabel.php │ ├── FormMultiCheckbox.php │ ├── FormNote.php │ ├── FormPassword.php │ ├── Form.php │ ├── FormRadio.php │ ├── FormReset.php │ ├── FormSelect.php │ ├── FormSubmit.php │ ├── FormTextarea.php │ ├── FormText.php │ ├── Gravatar.php │ ├── HeadLink.php │ ├── HeadMeta.php │ ├── HeadScript.php │ ├── HeadStyle.php │ ├── HeadTitle.php │ ├── HtmlElement.php │ ├── HtmlFlash.php │ ├── HtmlList.php │ ├── HtmlObject.php │ ├── HtmlPage.php │ ├── HtmlQuicktime.php │ ├── InlineScript.php │ ├── Interface.php │ ├── Json.php │ ├── Layout.php │ ├── Navigation │ │ ├── Breadcrumbs.php │ │ ├── HelperAbstract.php │ │ ├── Helper.php │ │ ├── Links.php │ │ ├── Menu.php │ │ └── Sitemap.php │ ├── Navigation.php │ ├── PaginationControl.php │ ├── Partial │ │ └── Exception.php │ ├── PartialLoop.php │ ├── Partial.php │ ├── Placeholder │ │ ├── Container │ │ │ ├── Abstract.php │ │ │ ├── Exception.php │ │ │ └── Standalone.php │ │ ├── Container.php │ │ ├── Registry │ │ │ └── Exception.php │ │ └── Registry.php │ ├── Placeholder.php │ ├── RenderToPlaceholder.php │ ├── ServerUrl.php │ ├── TinySrc.php │ ├── Translate.php │ ├── Url.php │ └── UserAgent.php ├── Interface.php └── Stream.php

6 directories,70 files

Zend_View和Zend_Controller的整合

主要在Zend_Controller_Action类中,

getInvokeArg('noViewRenderer') && $this->_helper->hasHelper('viewRenderer')) { return $this->view; } require_once 'Zend/View/Interface.php'; if (isset($this->view) && ($this->view instanceof Zend_View_Interface)) { return $this->view; } $request = $this->getRequest(); $module = $request->getModuleName(); $dirs = $this->getFrontController()->getControllerDirectory(); if (empty($module) || !isset($dirs[$module])) { $module = $this->getFrontController()->getDispatcher()->getDefaultModule(); } $baseDir = dirname($dirs[$module]) . DIRECTORY_SEPARATOR . 'views'; if (!file_exists($baseDir) || !is_dir($baseDir)) { require_once 'Zend/Controller/Exception.php'; throw new Zend_Controller_Exception('Missing base view directory ("' . $baseDir . '")'); } require_once 'Zend/View.php'; $this->view = new Zend_View(array('basePath' => $baseDir)); return $this->view; } /** * Render a view * * Renders a view. By default,views are found in the view script path as * /.phtml. You may change the script suffix by * resetting {@link $viewSuffix}. You may omit the controller directory * prefix by specifying boolean true for $noController. * * By default,the rendered contents are appended to the response. You may * specify the named body content segment to set by specifying a $name. * * @see Zend_Controller_Response_Abstract::appendBody() * @param string|null $action Defaults to action registered in request object * @param string|null $name Response object named path segment to use; defaults to null * @param bool $noController Defaults to false; i.e. use controller name as subdir in which to search for view script * @return void */ public function render($action = null,$name = null,$noController = false) { if (!$this->getInvokeArg('noViewRenderer') && $this->_helper->hasHelper('viewRenderer')) { return $this->_helper->viewRenderer->render($action,$name,$noController); } $view = $this->initView(); $script = $this->getViewScript($action,$noController); $this->getResponse()->appendBody( $view->render($script),$name ); } /** * Render a given view script * * Similar to {@link render()},this method renders a view script. Unlike render(),* however,it does not autodetermine the view script via {@link getViewScript()},* but instead renders the script passed to it. Use this if you know the * exact view script name and path you wish to use,or if using paths that do not * conform to the spec defined with getViewScript(). * * By default,the rendered contents are appended to the response. You may * specify the named body content segment to set by specifying a $name. * * @param string $script * @param string $name * @return void */ public function renderScript($script,$name = null) { if (!$this->getInvokeArg('noViewRenderer') && $this->_helper->hasHelper('viewRenderer')) { return $this->_helper->viewRenderer->renderScript($script,$name); } $view = $this->initView(); $this->getResponse()->appendBody( $view->render($script),$name ); }

Zend_View.php类

_useViewStream = (bool) ini_get('short_open_tag') ? false : true; if ($this->_useViewStream) { if (!in_array('zend.view',stream_get_wrappers())) { require_once 'Zend/View/Stream.php'; stream_wrapper_register('zend.view','Zend_View_Stream'); } } if (array_key_exists('useStreamWrapper',$config)) { $this->setUseStreamWrapper($config['useStreamWrapper']); } parent::__construct($config); } /** * Set flag indicating if stream wrapper should be used if short_open_tag is off * * @param bool $flag * @return Zend_View */ public function setUseStreamWrapper($flag) { $this->_useStreamWrapper = (bool) $flag; return $this; } /** * Should the stream wrapper be used if short_open_tag is off? * * @return bool */ public function useStreamWrapper() { return $this->_useStreamWrapper; } /** * Includes the view script in a scope with only public $this variables. * * @param string The view script to execute. */ protected function _run() { if ($this->_useViewStream && $this->useStreamWrapper()) { include 'zend.view://' . func_get_arg(0); } else { include func_get_arg(0); } } }

默认情况会自动通过Controller会通过render方法来实例化Zend_View,然后rener到对应的视图文件中。当然可以自己实例化Zend_View,然后使用。

action默认指向的文件是和action的名称相同,如果要指定视图文件,可以通过$this->render的相关方法指定.也可以通过addScriptPath和setScriptPath设置视图文件的目录。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读