2009年2月25日星期三

Re: [fw-mvc] Commons view helpers for every modules of an application

Thanks for your answer,

If I understand right your plugin, you rewrite part of the ViewRenderer
plugin to add the common path "on the top" of the plugin's directories
stack. There is no other shorter way for respecting the LIFO order of
views helpers, as using a modified view for the ViewRenderer and adding
a global path before he do is job ?

Hector Virgen a écrit :
> What I usually do is set up a controller plugin that adds the default
> helper path to the view prior to dispatching. Then, using the
> postDispatch hook, I add the current module's helper path to the stack
> so that helpers from both "default" and the current module become
> available.
>
> I'm not sure if this approach is "Zend Framework compliant" but it
> seems to work well for me.
>
> Here's a copy of the plugin I wrote to handle view directories:
>
> <?php
>
> class Virgen_Controller_Plugin_View extends
> Zend_Controller_Plugin_Abstract
> {
> protected $_view;
>
> protected $_viewRenderer;
>
> public function __construct()
> {
> // Set up view
> $this->_viewRenderer =
> Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
> $this->_view = $this->_viewRenderer->view;
>
> if (null === $this->_view) {
> $this->_viewRenderer->init();
> $this->_view = $this->_viewRenderer->view;
> }
>
> // Add default scripts/helpers to the script/helper paths
> $defaultViewPath = MODULE_PATH . DIRECTORY_SEPARATOR .
> 'default' . DIRECTORY_SEPARATOR . 'views';
> $this->_view->addScriptPath($defaultViewPath .
> DIRECTORY_SEPARATOR . 'scripts');
> $this->_view->addHelperPath($defaultViewPath .
> DIRECTORY_SEPARATOR . 'helpers', 'Virgen_View_Helper');
> }
>
> public function postDispatch(Zend_Controller_Request_Abstract
> $request)
> {
> // Determine module directory
> $front = Zend_Controller_Front::getInstance();
> $module = $request->getModuleName();
> $controllerDirectory = $front->getControllerDirectory($module);
> $moduleDirectory = dirname($controllerDirectory);
>
> $viewsPath = $moduleDirectory . DIRECTORY_SEPARATOR . 'views';
> $scriptsPath = $viewsPath . DIRECTORY_SEPARATOR . 'scripts';
> $helpersPath = $viewsPath . DIRECTORY_SEPARATOR . 'helpers';
>
> // Override script path with this module's script path
> $this->_view->setScriptPath($scriptsPath);
>
> // Add helpers to default helpers
> $helperPrefix = ('default' == $module) ? 'Virgen_View_Helper'
> : 'Virgen_' . ucfirst($module) . '_View_Helper';
> $this->_view->addHelperPath($helpersPath, $helperPrefix);
> }
> }
>
> -Hector
>
>
> On Mon, Feb 23, 2009 at 2:43 PM, Lucas Corbeaux
> <lucas.corbeaux@gmail.com <mailto:lucas.corbeaux@gmail.com>> wrote:
>
> Hi there,
>
> I have a little design question as I want to be the most "Zend
> Framework compliant" as possible in my application design.
>
> I have three modules, and both uses the same view helpers. If I
> put my helpers in default/views/helpers/, it works perfectly for
> the default module, but I must use a
> $this->view->addHelper('document_root/../application/default/views/helpers/)
> in my others modules' init method (wich is not really complex, as
> all my modules controller inherit from an abstract one, inheriting
> themselves from a much global one for all the application).
>
> Is there a best practice, or, at least, a more common way to do
> for such a configuration (Custom view class, Front plugin...) ? So
> many way, so hard to choose one ;)
>
> Thanks,
> Lucas
>
>

没有评论: