<?PHP//1. View RendererclassPhprendererImplementsRenderer,treerendererinterface{/** * Plugin manager*/ Public functionGethelperpluginmanager () {if(NULL===$this->__helpers) {//false $this->sethelperpluginmanager (NewHelperpluginmanager ()); } return $this-__helpers; } /** * Get plugin*/ Public functionPlugin$name,Array $options=NULL) { //zend\mvc\service\viewhelpermanagerfactory//Zend\view\helperpluginmanager return $this->gethelperpluginmanager ()->get ($name,$options); } /** * Rendering*/ Public functionRender$nameOrModel,$values=NULL) { // .... Try { Ob_start(); $includeReturn=include $this->__file;//Execute Template $this->__content =Ob_get_clean(); } Catch(\Exception $ex) { Ob_end_clean(); Throw $ex; } // .... }}//2. Plugin Manager//Plugin manager Create factoryclassViewhelpermanagerfactoryextendsabstractpluginmanagerfactory{ConstPlugin_manager_class = ' Zend\view\helperpluginmanager '; protected $defaultHelperMapClasses=Array( ' Zend\form\view\helperconfig ', ' zend\i18n\view\helperconfig ', ' zend\navigation\view\helperconfig ' ); Public functionCreateService (servicelocatorinterface$serviceLocator) { $plugins= Parent::createservice ($serviceLocator); //default configuration for injection plug-in manager foreach($this->defaulthelpermapclasses as $configClass) { if(is_string($configClass) &&class_exists($configClass)) { $config=New $configClass; $config->configureservicemanager ($plugins); } } //URL plugin creation factory Configure URL View helper with router $plugins->setfactory (' url ',function() Use($serviceLocator) { $helper=NewViewhelper\url;//Zend\view\helper\url $router= Console::isconsole ()? ' Httprouter ': ' Router '; $helper->setrouter ($serviceLocator->get ($router));//injecting a Routing object $match=$serviceLocator->get (' Application ') -getmvcevent ()-Getroutematch (); if($matchinstanceof Routematch) { $helper->setroutematch ($match);//inject a matching route object } return $helper; }); //BasePath plug-in creation factory $plugins->setfactory (' BasePath ',function() Use($serviceLocator) { //... return $basePathHelper; }); //DOCTYPE plug-in creation factory $plugins->setfactory (' doctype ',function() Use($serviceLocator) { //... return $doctypeHelper; }); }}//plug-in ManagerclassServiceManagerImplementsservicelocatorinterface{ Public function__construct (configinterface$config=NULL) { if($config) { $config->configureservicemanager ($this); } }}Abstract classAbstractpluginmanagerextendsServiceManagerImplementsservicelocatorawareinterface{ Public function__construct (configinterface$configuration=NULL) {Parent:: __construct ($configuration); $self=$this; $this->addinitializer (function($instance) Use($self) {//!!! Inject plug-in manager into plug-in objects if($instanceinstanceof Servicelocatorawareinterface) { $instance->setservicelocator ($self); } }); }}classHelperpluginmanagerextendsabstractpluginmanager{protected $factories=Array( ' Flashmessenger ' = ' zend\view\helper\service\flashmessengerfactory ', ' identity ' = ' zend\view\helper\s ' Ervice\identityfactory ', ); protected $invokableClasses=Array( ' BasePath ' = ' zend\view\helper\basepath ', ' cycle ' = ' zend\view\helper\cycle ', ' Declarevars ' = ' zend\view\helper\declarevars ', ' doctype ' = ' Zend\view\helper\doctyp E ',// overridden by a factory in Viewhelpermanagerfactory ' escapehtml ' = ' zend\view\helper\escapehtml ', ' escapehtmlattr ' = ' zend\view\helper\es Capehtmlattr ', ' escapejs ' = ' zend\view\helper\escapejs ', ' escapecss ' = ' zend\vi ' Ew\helper\escapecss ', ' escapeurl ' = ' zend\view\helper\escapeurl ', ' gravatar ' = ' Zend\view\helper\gravatar ', ' htmltag ' = ' zend\view\helper\htmltag ', ' headlink ' = > ' Zend\view\helper\headlink ', ' headmeta ' = ' zend\view\helper\headmeta ', ' headscript ' = ' Zend\view\helper\headscript ', ' headstyle ' = ' zend\view\helper\headstyle ', ' headtit Le ' = ' zend\view\helper\headtitle ', ' htmlflash ' = ' zend\view\helper\htmlflash ', ' Htmllist ' = ' zend\view\helper\htmllist ', ' htmlobject ' = ' zend\view\helper\htmlobject ', ' HtmlPage ' = ' zend\view\helper\htmlpage ', ' htmlquicktime ' = ' zend\view\helper\htmlquicktime ', ' Inli Nescript ' = ' zend\view\helper\inlinescript ', ' json ' = ' Zend\view\helper\json ', ' Layout ' = ' zend\view\helper\layout ', ' paginationcontrol ' = ' Zend\view\helper\paginationcon ' Trol ', ' partialloop ' = ' zend\view\helper\partialloop ', ' partial ' = ' zend\view\he Lper\partial ', ' placeholder ' = ' zend\view\helper\placeholder ', ' renderchildmodel ' = ' Zend ' \view\helper\renderchildmodel ', ' rendertoplaceholder ' = ' zend\view\helper\rendertoplaceholder ', ' Serveru RL ' = ' zend\view\helper\serverurl ', ' url ' = ' zend\view\helper\url ', ' VIEWM Odel ' = ' Zend\view\helper\viewmodel ', ); Public function__construct (configinterface$configuration=NULL) {Parent:: __construct ($configuration); $this->addinitializer (Array($this, ' Injectrenderer '))//injecting a view renderer to a plug-in object->addinitializer (Array($this, ' Injecttranslator '));//injecting a language translator into a plugin object } //View Renderer Public functionGetrenderer () {return $this-renderer; }}?>//3. Use of the Plugin Manager//In the template moduel1/ctrl1/action1.phtml<div>hello,this is view plugin demo:</div><?PHP//View_model View Plugin $helper=$this->plugin (' View_model '); $helper->setcurrent ($model); //URL View plugin//case.0 $helper=$this->plugin (' url '); Echo $helper(' album ',Array(' action ' = ' + ' Add ')); //case.1 Echo $this->url (' album ',Array(' action ' = ' + ' Add ')); //BasePath View Plugin $helper=$this->plugin (' BasePath '); $helper->setcurrent ($model); ?>
ZendFramework-2.4 source code-about Mvc-view Layer-view renderer, view plug-in Manager