2011年3月5日星期六

Re: [fw-mvc] Re: troubles with custom view-scripts location and noViewRender=true by default

On Sat, Mar 5, 2011 at 2:11 AM, jonnott <jonnott@gmail.com> wrote:

> Is there another way to state this setting globally without using an
> INI file? The problem is that I need to generate this path *dynamically*, as
> the ZF entry-point is within a shared codebase.


Yes, you can bootstrap your view instance and then pull it from the
bootstrap to modify it. This ensures that you are accessing and modifying
the same view instance throughout your application.

To make sure your view is bootstrapped, add at least one configuration
option to application.ini. If you want to use all the defaults, then pass in
an empty config:

# application/configs/application.ini
; Bootstraps the view with default values
resources.view[] =

Then, you can pull the view from the bootstrap from within an action
controller like this:

# application/controllers/IndexController.php
public function init()
{
$bootstrap = $this->getInvokeArg('bootstrap');
$view = $bootstrap->getResource('view');
}

>From that point you can then add your custom view script paths:

$view->addScriptPath('path/to/my/view/scripts');

Would this work at the Zend_Controller_Action::preDispatch() level, or
> is that too late? (assuming the above would normally be in the front
> controller plugin routeShutdown() method).


Automatic view script rendering occurs during postDispatch, so swapping
paths in preDispatch or even within your action body should work. In my
example above I used init, which occurs before preDispatch.

My thinking would be to create a base action controller with this in
> preDispatch() which I can subclass for whatever action controllers need this
> logic.


If you find that you need to provide this functionality in more than one
action controller, I suggest using action helpers instead of creating a base
action controller. The problem with base action controllers is that they
tend to get bloated with some functionality for controllers A, B, and C and
other functionality for controllers D, E, and F. Maintaing this can become
problematic and often requires a large ugly switch.

Action helpers allow you to take that bit of functionality and encapsulate
it in its own class, allowing you to use it on-demand from any action
controller.

For more info on action helpers check out the docs:

http://framework.zend.com/manual/en/zend.controller.actionhelpers.html

Will this work just as well with inverted logic/booleans,
> i.e. Zend_Controller_Front->setParam('noViewRenderer')=true; and
> then $viewRenderer->setNoRender(false); in the routeShutdown() or
> action controller preDispatch()


Setting the param "noViewRenderer" to true prevents the front controller
from registering the view renderer helper entirely, but in your case I
believe you want it registered but simply not performing any automatic view
script rendering.

So I would go with my original suggestion of leaving the view renderer there
(because you might need it), disabling automatic view rendering (a property
of the view renderer itself), and then re-enabling it when required.

--
*Hector Virgen*
Sr. Web Developer
http://www.virgentech.com

没有评论: